node. JS Common Express method

Source: Internet
Author: User

node. JS Manual Query-express method

1. Send method

The Send method sends a response message to the browser, and can intelligently handle different types of data that the Send method automatically makes when the response is output, such as head information, HTTP cache support, and so on, such as String, Array, Object, number. When the parameter is a string, Content-type defaults to "text/html" when the parameter is an array or an object, express returns a JSON when the parameter is a numberexpress will help you set a response body, For example: 200

2. Common methods for obtaining parameters

(1), Req.body

(2), req.query

(3), Req.params

(i), req.body example

Body is not Nodejs by default, you need to load Body-parser middleware to use Req.body, this method is usually used to parse the data in the POST request

<form action='/test'Method='Post'> <input type='text'Name='name'Value='LMW'> <input type='text'Name='Tel'Value='1234567'> <input type='Submit'Value='Submit'> </form>App.post ('/test', Function (req, res) {Console.log (req.body.name); Console.log (Req.body.tel);});

(ii), req.query examples

Nodejs is provided by default and does not need to be loaded into the middleware, which is often used to parse the data in a GET request

get/test?name=lmw&tel=123456789app.get('/test' ) , Function (req, res) {    console.log (req.query.name);    Console.log (Req.query.tel);});


(iii), Req.query and req.body at the same time

<form action='/test?id=1'Method='Post'> <input type='text'Name='name'Value='LMW'> <input type='text'Name='Tel'Value='123456789'> <input type='Submit'Value='Submit'> </form>App.post ('/test', Function (req, res) {Console.log (req.query.id);    Console.log (Req.body.name); Console.log (Req.body.tel);});

(iv), Req.params

Another way to pass parameters to the server, but this is not the traditional standard practice, is an extension of the HTTP Routing application

get/test/lmw/123456789app.get('/test/:name/:tel', Function (req, res) {    console.log (req.params. Name);    Console.log (req.Params. Tel);});

node. JS Common Express method

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.