从零开始学习Node.js系列教程四:多页面实现的数学运算示例
程序员文章站
2022-04-28 09:23:53
本文实例讲述了node.js多页面实现的数学运算。分享给大家供大家参考,具体如下:
app-node.js
var http_port = 3000;
var...
本文实例讲述了node.js多页面实现的数学运算。分享给大家供大家参考,具体如下:
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><h1>{title}</h1>", "<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} * {b} = {result}</p>" .replace("{a}", req.a) .replace("{b}", 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程序设计有所帮助。
下一篇: C/C++结构体有效对齐值的确定