This article describes how to create a web calculator using node. js + express. For more information, see
Environment:
Host: WIN10
Express installation:
1. install express-generator
Enter the following command:
npm install -g express-generator
2. install express
Enter the following command:
npm install -g express
3. verify whether the installation is successful
Enter the command: express-V
View help: express -- help
Project creation:
express -e calculatorcd calculator && npm install
Run the default webpage:
Run npm start or node./bin/www.
The port is configured in/bin/www.
You can perform addition operations.
Source code:
View/index. ejs: add input box
<%= title %>
Routes/index. js: calculates the submitted data and pushes the results.
Var express = require ('Express '); var router = express. router ();/* GET home page. */router. get ('/', function (req, res, next) {res. render ('index', {title: 'calculator V1.0 by jdh ', numa: 0, numb: 0, sum: 0}) ;}); router. post ('/', function (req, res) {console. log ("receive:", req. body. num1, req. body. num2); var sum = parseFloat (req. body. num1) + parseFloat (req. body. num2); console. log ('sum = ', sum); res. render ('index', {title: 'calculator 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;