Home > Web Front-end > JS Tutorial > body text

Node.js+express makes web calculator

PHPz
Release: 2018-10-13 17:04:40
Original
1659 people have browsed it

Environment:

Host: WIN10

express installation:

1. Install express-generator

Enter the command:

npm install -g express-generator
Copy after login

2. Install express

Enter the command:

npm install -g express
Copy after login

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
Copy after login

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;
Copy after login

[Related tutorial recommendations]

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template