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 === 'https://www.jb51.net/'){ require('./home-node').get(req, res); }else if (req.requrl.pathname === 'https://www.jb51.net/square'){ require('./square-node').get(req, res); }else if (req.requrl.pathname === 'https://www.jb51.net/factorial'){ require('./factorial-node').get(req, res); }else if (req.requrl.pathname === 'https://www.jb51.net/fibonacci'){ require('./fibo-node').get(req, res); }else if (req.requrl.pathname === 'https://www.jb51.net/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 :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>", "<p><a href='https://www.jb51.net/'>home</a></p>", "<p><a href='https://www.jb51.net/mult'>Multiplication</a></p>", "<p><a href='https://www.jb51.net/square'>Square</a></p>", "<p><a href='https://www.jb51.net/factorial'>Factorial</a></p>", "<p><a href='https://www.jb51.net/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>{a} squared = {sq}</p>" .replace("{a}", req.a) .replace("{sq}", req.a * req.a)) : ""), "<p>Enter numbers to see its square</p>", "<form action='https://www.jb51.net/square' method='get'>", "A: <input type='text' /><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>{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 action='https://www.jb51.net/factorial' method='get'>", "A: <input type='text' /><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>{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 action='https://www.jb51.net/mult' method='get'>", "A: <input type='text' /><br/>", "B: <input type='text' /><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>{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 action='https://www.jb51.net/fibonacci' method='get'>", "A: <input type='text' /><br/>", "<input type='submit' value='Submit' />", "</form>" ].join('\n')) ); }
fibo2-node.js