Node. js + express makes a webpage calculator, node. jsexpress

Source: Internet
Author: User
Tags mongodb server

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

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.