Home >Web Front-end >JS Tutorial >Node.js+express makes web calculator

Node.js+express makes web calculator

PHPz
PHPzOriginal
2016-05-16 15:19:331815browse

Environment:

Host: WIN10

express installation:

1. Install express-generator

Enter the command:

npm install -g express-generator

2. Install express

Enter the command:

npm install -g express

3 .Verify whether the installation is successful

Enter the command: express -V

View help: express --help

Create the project:

express -e calculator
cd calculator && npm install

Run the default web page:

Enter the command: npm start or node ./bin/www

The port is configured in /bin/www.

Can perform addition operations.

Source code:

view/index.ejs: Add input box

routes/index.js: Calculate and push the submitted data Result

var express = require('express'); 
var router = express.Router(); 
 
/* GET home page. */ 
router.get('/', function(req, res, next) { 
 res.render('index', {  
  title: '计算器V1.0 by jdh', 
  numa: 0, 
    numb: 0, 
    sum: 0 
 }); 
}); 
 
router.post('/', function (req, res) { 
  console.log("接收:", req.body.num1, req.body.num2); 
  var sum = parseFloat(req.body.num1) + parseFloat(req.body.num2); 
  console.log('sum = ',sum); 
   
  res.render('index', {  
  title: '计算器V1.0 by jdh', 
// numa: req.body.num1, 
//   numb: req.body.num2 
    numa: req.body.num1, 
    numb: req.body.num2, 
    sum: sum 
 }); 
}); 
   
module.exports = router;

[Related tutorial recommendations]

1. JavaScript video tutorial
2. JavaScript online manual
3. bootstrap tutorial

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn