Node. js learning: Using express to build a simple web Calculator
Node. js learning: Using express to build a simple web Calculator
Blog link:Http://blog.csdn.net/jdh99, Author: jdh, reprinted please note.
Environment:
HOST: WIN10
Express installation:
1. Install express-generator
Run npm install-g express-generator.
2. Install express
Run npm install-g express.
3. verify whether the installation is successful
Enter the command: express-V
View help: express -- help
Project Creation:
Express-e calculator
Cd calculator & npm install
Run the default webpage:
Run npm start or node./bin/www.
The port is configured in/bin/www.
Simple Web calculator effect:
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. num2numa: req. body. num1, numb: req. body. num2, sum: sum}) ;}); module. exports = router;