Use NodeJS + Express to take a value from Get/post Request-excerpt from the network

Source: Internet
Author: User

Regardless of which web-site application has been developed, the beginner's first reference to the starting hand, eight or nine is the value of Get/post Request. However, in the world of node. js + Express, it seems that everyone is a master, born to use, never seen someone write.

This should be the opening of the Web Service, in the Client and Server interaction, the browser out of the Get/post Request will be the value of server-side, the common application is the page with the POST method sent out of the form, or the web Query Strings (ex:page?page=3&id=5) on the Address column. Then our web site is programmed to parse these parameters and get the information from the user.

Obtain the Get Request Query Strings:

GET/Test?Name=Fred&Tel=0926xxx572Appget ( '/test ' , function (req, res< Span class= "pun" >)  { Console. (req. Query. Name Console. (req. Query. Tel});             

If it is through the form and is using the POST method:

<form Action='/test ' Method=' Post '> <input Type=' Text ' Name=' Name ' Value=' Fred '> <input type< Span class= "pun" >= ' text '  name=  ' tel '  value=> <inputtype= ' submit '  value< Span class= "pun" >= ' Submit ' > </form >app.post ('/test ', function (req, res) {Console.log (req.query.id); Console.log (Req.body.name); Console.log (Req.body.tel);               

It is also possible to Query the Strings and POST method forms simultaneously using:

<form Action='/test?id=3 ' Method=' Post '> <input Type=' Text ' Name=' Name ' Value=' Fred '> <input type< Span class= "pun" >= ' text '  name=  ' tel '  value=> <inputtype= ' submit '  value< Span class= "pun" >= ' Submit ' > </form >app.post ('/test ', function (req, res) {Console.log (req.query.id); Console.log (Req.body.name); Console.log (Req.body.tel);               

And there's another way to do that is to pass the parameters to the Server, that is, using the path, you can use the HTTP Routing of the Web Server to parse, often see the various kinds of web Framework. This is not a standardized practice, it is an extension of the HTTP Routing.

GET/Hello/Fred/0926xxx572App.get('/hello/:name/:tel ', function(req, res) { console . Log(req.  params.  Name); console.  Log(req.  params.  Tel); });     

Using NodeJS + Express to take value from Get/post Request-excerpt from the network

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.