本文實例講述了Node.js多頁面實現(xiàn)的數(shù)學(xué)運算。分享給大家供大家參考,具體如下:
張家川回族自治網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項目制作,到程序開發(fā),運營維護(hù)。成都創(chuàng)新互聯(lián)公司自2013年創(chuàng)立以來到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)公司。
app-node.js
var http_port = 3000; var http = require('http'); var htutil = require('./htutil'); var server = http.createServer(function(req, res){ htutil.loadParams(req, res, undefined); if (req.requrl.pathname === '/'){ require('./home-node').get(req, res); }else if (req.requrl.pathname === '/square'){ require('./square-node').get(req, res); }else if (req.requrl.pathname === '/factorial'){ require('./factorial-node').get(req, res); }else if (req.requrl.pathname === '/fibonacci'){ require('./fibo-node').get(req, res); }else if (req.requrl.pathname === '/mult'){ require('./mult-node').get(req, res); }else{ res.writeHead(404, {'Content-Type': 'text/plain'}); res.end("bad URL" + req.url); } }); server.listen(http_port); console.log('listening to http://localhost:3000');
htutil.js
var url = require('url'); exports.loadParams = function(req, res, next){ req.requrl = url.parse(req.url, true); req.a = (req.requrl.query.a && !isNaN(req.requrl.query.a)) ? new Number(req.requrl.query.a) : NaN; req.b = (req.requrl.query.b && !isNaN(req.requrl.query.b)) ? new Number(req.requrl.query.b) : NaN; if(next) next(); } exports.navbar = function(){ return ["<div class='navbar'>", "<p><a href='/'>home</a></p>", "<p><a href='/mult'>Multiplication</a></p>", "<p><a href='/square'>Square</a></p>", "<p><a href='/factorial'>Factorial</a></p>", "<p><a href='/fibonacci'>Fibonacci</a></p>", "</div>"].join('\n'); } exports.page = function(title, navbar, content){ return ["<html><head><title>{title}</title></head>", "<body><h2>{title}</h2>", "<table><tr>", "<td>{navbar}</td><td>{content}</td>", "</tr></table></body></html>" ].join('\n') .replace(new RegExp("{title}", "gm"), title) .replace("{navbar}", navbar) .replace("{content}", content); }
home-node.js
var htutil = require('./htutil'); exports.get = function(req, res){ res.writeHead(200, {'Content-Type': 'text/html'}); res.end(htutil.page("Math Wizard", htutil.navbar(), "<p>Math Wizard</p>")); }
square-node.js
var htutil = require('./htutil'); exports.get = function(req, res){ res.writeHead(200, {'Content-Type': 'text/html'}); var result = req.a * req.a; res.end( htutil.page("Square", htutil.navbar(), [ (!isNaN(req.a) ? ("<p class='result'>{a} squared = {sq}</p>" .replace("{a}", req.a) .replace("{sq}", req.a * req.a)) : ""), "<p>Enter numbers to see its square</p>", "<form name='square' action='/square' method='get'>", "A: <input type='text' name='a' /><br/>", "<input type='submit' value='Submit' />", "</form>" ].join('\n')) ); }
factorial-node.js
var htutil = require('./htutil'); var math = require('./math'); exports.get = function(req, res){ res.writeHead(200, {'Content-Type': 'text/html'}); res.end( htutil.page("Factorial", htutil.navbar(), [ (!isNaN(req.a) ? ("<p class='result'>{a} factorial = {fact}</p>" .replace("{a}", req.a) .replace("{fact}", math.factorial(Math.floor(req.a)))) : ""), "<p>Enter numbers to see its factorial</p>", "<form name='factorial' action='/factorial' method='get'>", "A: <input type='text' name='a' /><br/>", "<input type='submit' value='Submit' />", "</form>" ].join('\n')) ); }
mult-node.js
var htutil = require('./htutil'); exports.get = function(req, res){ res.writeHead(200, {'Content-Type': 'text/html'}); var result = req.a * req.b; res.end( htutil.page("Multiplication", htutil.navbar(), [ (!isNaN(req.a) && !isNaN(req.b) ? ("<p class='result'>{a} * = {result}</p>" .replace("{a}", req.a) .replace("", req.b) .replace("{result}", req.a * req.b)) : ""), "<p>Enter numbers to mutiply</p>", "<form name='mult' action='/mult' method='get'>", "A: <input type='text' name='a' /><br/>", "B: <input type='text' name='b' /><br/>", "<input type='submit' value='Submit' />", "</form>" ].join('\n')) ); }
fibo-node.js
var htutil = require('./htutil'); var math = require('./math'); exports.get = function(req, res){ res.writeHead(200, {'Content-Type': 'text/html'}); res.end( htutil.page("Fibonacci", htutil.navbar(), [ (!isNaN(req.a) ? ("<p class='result'>{a} fibonacci = {fibo}</p>" .replace("{a}", Math.floor(req.a)) .replace("{fibo}", math.fibonacci(Math.floor(req.a)))) : ""), "<p>Enter numbers to see its fibonacci</p>", "<form name='fibonacci' action='/fibonacci' method='get'>", "A: <input type='text' name='a' /><br/>", "<input type='submit' value='Submit' />", "</form>" ].join('\n')) ); }
fibo2-node.js
var htutil = require('./htutil'); var math = require('./math'); function sendResult(req, res, a, fiboval){ res.writeHead(200, {'Content-Type': 'text/html'}); res.end( htutil.page("Fibonacci", htutil.navbar(), [ (!isNaN(fiboval) ? ("<p class='result'>{a} fibonacci = {fibo}</p>" .replace("{a}", a) .replace("{fibo}", fiboval)) : ""), "<p>Enter numbers to see its fibonacci</p>", "<form name='fibonacci' action='/fibonacci' method='get'>", "A: <input type='text' name='a' /><br/>", "<input type='submit' value='Submit' />", "</form>" ].join('\n')) ); } exports.get = function(req, res){ if (!isNaN(req.a)){ math.fibonacciAsync(Math.floor(req.a), function(val){ sendResult(req, res, Math.floor(req.a) ,val); }); }else { sendResult(req, res, NaN, NaN); } }
math.js
var factorial = exports.factorial = function(n){ if (n == 0) return 1; else return n * factorial(n-1); } var fibonacci = exports.fibonacci = function(n){ if (n === 1) return 1; else if (n === 2) return 1; else return fibonacci(n-1) + fibonacci(n-2); } var fibonacciAsync = exports.fibonacciAsync = function(n, done){ if (n ==1 || n == 2) done(1); else { process.nextTick(function(){ fibonacciAsync(n-1, function(val1){ process.nextTick(function(){ fibonacciAsync(n-2, function(val2){ done(val1 + val2); }); }) }); }); } }
希望本文所述對大家nodejs程序設(shè)計有所幫助。
分享文章:從零開始學(xué)習(xí)Node.js系列教程四:多頁面實現(xiàn)的數(shù)學(xué)運算示例
瀏覽路徑:http://jinyejixie.com/article34/gpgose.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、全網(wǎng)營銷推廣、外貿(mào)網(wǎng)站建設(shè)、電子商務(wù)、網(wǎng)站設(shè)計公司、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)