Node. js + express makes a webpage calculator, node. jsexpress
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
<! DOCTYPE html>
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;
Articles you may be interested in:
- Getting started with node. js Web application framework Express
- Express author TJ say goodbye to Node. js and rush to Go
- Getting started with Node. js, Express, Ejs, Mongodb server and application development
- Install and configure the node. js + express development environment in win7