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

Node.js+express makes web calculator

May 16, 2016 pm 03:19 PM
express node.js calculator

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

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What key is ac on the calculator? What key is ac on the calculator? Feb 24, 2023 am 10:19 AM

What key is ac on the calculator?

Teach you how to open the win10 calculator Teach you how to open the win10 calculator Jul 12, 2023 pm 11:21 PM

Teach you how to open the win10 calculator

An article about memory control in Node An article about memory control in Node Apr 26, 2023 pm 05:37 PM

An article about memory control in Node

Detailed graphic explanation of the memory and GC of the Node V8 engine Detailed graphic explanation of the memory and GC of the Node V8 engine Mar 29, 2023 pm 06:02 PM

Detailed graphic explanation of the memory and GC of the Node V8 engine

How to use express to handle file upload in node project How to use express to handle file upload in node project Mar 28, 2023 pm 07:28 PM

How to use express to handle file upload in node project

Let's talk in depth about the File module in Node Let's talk in depth about the File module in Node Apr 24, 2023 pm 05:49 PM

Let's talk in depth about the File module in Node

What should I do if node cannot use npm command? What should I do if node cannot use npm command? Feb 08, 2023 am 10:09 AM

What should I do if node cannot use npm command?

Let's talk about the event loop in Node Let's talk about the event loop in Node Apr 11, 2023 pm 07:08 PM

Let's talk about the event loop in Node

See all articles