Web App for node. JS GET, POST

Source: Internet
Author: User

a simple HTTP server, route processing, Get and Post, Get instance, Post Instance

1. Simple HTTP Server

  

Create an HTTP server, get and output the request URL, method, header, and make different output according to the requested resource.

    

   

Console command: Node app.js

  

Browser input: 127.0.0.1:8080 returns 404, output can not find source

        

        

        127.0.0.1:8080/index

        

        

        127.0.0.1:8080/img

        

        

        

2. Route handling

3. Get Instances

The HTTP client send to get request parameter data in node. js is stored in the URL property of the Request object.

such as: http://127.0.0.1:8080/login?name=zhang&pwd=123

Where the URL path is named login

Use the GET method to expose data name=zhang&pwd=123 to the address bar.

The parse method in the node. JS Native URL module Gets the get parameters for HTTP.

Url.parse (Req.url). Pathname.

The code obtains the request path in the URL based on the Req object, such as login,

Req.url to login?name=zhang&pwd=123

var str=url.parse (req.url). Query

Get name=zhang&pwd=123

  

var param = querystring.parse ('name=zhang&pwd=123')

Get the JSON object {name: ' Zhang ', pwd: ' 123 '}

Get name and PWD can use Param.name or param[' name ']

  

Example: Use the HTTP module to create a server that receives arbitrary URL requests and passes parameters using the Get method.

The server receives a client request URL, outputs a path name for each request, and a JSON object that requests parameters.

  

  

  

4. Post instance

  

  

Http://127.0.0.1:8080/add

  

  

  

Web App for node. JS GET, POST

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.