Node. js learning: Using express to build a simple web Calculator

Source: Internet
Author: User

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;




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.