DDR爱好者之家 Design By 杰米
本文实例讲述了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)) "<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程序设计有所帮助。
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
更新日志
2024年11月27日
2024年11月27日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]