189 8069 5689

从零开始学习Node.js系列教程四:多页面实现的数学运算示例

本文实例讲述了Node.js多页面实现的数学运算。分享给大家供大家参考,具体如下:

张家川回族自治网站制作公司哪家好,找成都创新互联公司!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设等网站项目制作,到程序开发,运营维护。成都创新互联公司自2013年创立以来到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联公司

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 [""].join('\n');
}
exports.page = function(title, navbar, content){
  return ["{title}",
    "

{title}

", "", "", "
{navbar}{content}
" ].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(), "

Math Wizard

")); }

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) ?
        ("

{a} squared = {sq}

" .replace("{a}", req.a) .replace("{sq}", req.a * req.a)) : ""), "

Enter numbers to see its square

", "
", "A:
", "", "
" ].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) ?
        ("

{a} factorial = {fact}

" .replace("{a}", req.a) .replace("{fact}", math.factorial(Math.floor(req.a)))) : ""), "

Enter numbers to see its factorial

", "
", "A:
", "", "
" ].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) ?
        ("

{a} * {b} = {result}

" .replace("{a}", req.a) .replace("{b}", req.b) .replace("{result}", req.a * req.b)) : ""), "

Enter numbers to mutiply

", "
", "A:
", "B:
", "", "
" ].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) ?
        ("

{a} fibonacci = {fibo}

" .replace("{a}", Math.floor(req.a)) .replace("{fibo}", math.fibonacci(Math.floor(req.a)))) : ""), "

Enter numbers to see its fibonacci

", "
", "A:
", "", "
" ].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) ?
        ("

{a} fibonacci = {fibo}

" .replace("{a}", a) .replace("{fibo}", fiboval)) : ""), "

Enter numbers to see its fibonacci

", "
", "A:
", "", "
" ].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);
          });
        })
      });
    });
  }
}

从零开始学习Node.js系列教程四:多页面实现的数学运算示例 

从零开始学习Node.js系列教程四:多页面实现的数学运算示例 

从零开始学习Node.js系列教程四:多页面实现的数学运算示例

希望本文所述对大家nodejs程序设计有所帮助。


网站题目:从零开始学习Node.js系列教程四:多页面实现的数学运算示例
新闻来源:http://cdxtjz.cn/article/gpgose.html

其他资讯