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