Node. Js Http modules
Http module, the main application is two parts:
- Http.createserver acts as a Web server
- Http. createClient, which acts as a client and implements crawlers and the like.
The Http server
Create a simple HTTP server
The Nodejs website provides the following example code:
const http = require('http')
const hostname = '127.0.0.1'
const port = 3000
const server = http.createServer((req, res) = > {
res.statusCode = 200
res.setHeader('Content-Type'.'text/plain') //writeHead, 200 indicates that the page is normal, and text/plain indicates that the page is text.
res.end('Hello World\n') // end completes the write
})
server.listen(port, hostname, () = > {
console.log(The server runs in http://${hostname}:${port}`)})Copy the code
Analyze the code:
- First of all, through
require('http')
The introduction ofhttp
The module. - And then, through
http.createServer([requestListener])
To create a Web server and pass in an optional callback function with two parameters representing the client request objectrequest
And the server-side response objectresponse
. - Finally, use
server.listen([port][, hostname][, backlog][, callback])
, start at the specifiedport
andhostname
The accepthttp
Request and respond
Through the above three steps, you have created a simple HTTP server.
Shut down the server
How do I shut down the HTTP server I just created?
// Stop the server from receiving new connections
server.close([callback])
Copy the code
timeout
The Node.js Http module also provides server.timeout for viewing or setting a timeout
server.timeout = 1000 // Set the timeout to 1 second
console.log(server.timeout)
Copy the code
The request object
- Request. url: Indicates the URL requested by the client
- Request. headers: HTTP header requested by the client
- Request. Method Indicates the method of obtaining a request. There are several options, such as POST,GET, and DELETE.
- Request. HttpVersion: Indicates the HTTP version
- Request. Trailers, for some additional HTTP headers
- Request. socket, the socket object used to listen for client requests
We can write a simple JS code to record the client request information. Create a file named simpleHttpServer.js that might look like this:
const http = require('http')
const fs = require('fs')
const server = http.createServer((req, res) = > {
res.statusCode = 200
res.setHeader('Content-Type'.'text/plain')
if(req.url === '/') {let logfile = fs.createWriteStream('./log.txt')
logfile.write('Request method:${req.method} \r\n`)
logfile.write('Request URL:${req.url} \r\n`)
logfile.write('Request header object:The ${JSON.stringify(req.headers, null.4)} \r\n`)
logfile.write('Request HTTP version:${req.httpVersion} \r\n`)
}
res.end('Hello World\r\n')
})
server.listen(3000.'127.0.0.1'.() = > {
console.log('Server running at http://127.0.0.1:3000')})Copy the code
The contents of log.txt might look like this:
GET request URL: / Request header object: {"host": "localhost:3000", "connection": "keep-alive", "upset-insecure -requests": "1", "user-agent": "Mozilla/5.0 (Windows NT 6.1; Win64; X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36", "Accept ": "text/html,application/xhtml+xml,application/xml; Q = 0.9, image/webp image/apng, * / *; Q = 0.8, application/signed - exchange; v=b3", "accept-encoding": "gzip, deflate, br", "accept-language": "zh-CN,zh; Q = 0.9 ", "cookie", "Webstorm - ab681485 = f7ada1cf - 810 - b - 404 - b - 9211-5 cd0460e5013; OptimizelyEndUserId = oeu1506523580880r0. 9072636112603187; _ga = GA1.1.744315833.1506431133 "} requests HTTP version: 1.1Copy the code
The response object
- response.writeHead(statusCode, [reasonPhrase], [headers])
- Response. statusCode, the HTML page status value
- Response. header, the HTTP header returned, can be either a string or an object
- Response.settimeout (msecs, callback) : Sets the time to be returned by HTTP timeout. Once the time is exceeded, the connection will be discarded
- Response. statusCode, set the returned page statusCode
- Responsetheader (name, value), which sets the HTTP header
- Response. headersSent checks whether the HTTP header is set
- Response.write (chunk, [encoding]), the returned web page data, [encoding] the default is UTF-8
- Response.end ([data], [encoding]), the response ends
URL parsing
In Node.js, a URL module and a QueryString module are provided
The QueryString module is used for URL processing and parsing
querystring.parse(str, [sep], [eq], [options])
Such as:
querystring.parse('foo=bar&baz=qux&baz=quux&corge')
// returns
{ foo: 'bar'.baz: ['qux'.'quux'].corge: ' ' }
Copy the code
querystring.stringify(obj,separator,eq,options)
This method serializes an object into a string, as opposed to QueryString.parse. Such as:
querystring.stringify({name: 'whitemu'.sex: [ 'man'.'women']});// returns
'name=whitemu&sex=man&sex=women'
Copy the code
URL form
The URL module provides utility functions for URL handling and parsing. A URL string is a structured string that contains multiple meaningful parts. When parsed, a URL object is returned that contains each component as an attribute.
The following details describe each component of a parsed URL
- Format (urlObject) returns a url string formatted from urlObject
- Url.parse (urlString[, parseQueryString[, slashesDenoteHost]]), parses a URL string and returns a URL object
We can use the object parsed by url.parse() to get the individual values in the URL
For more information, please refer to Nodejs Chinese official website
Build a simple HTTP JSON API Server
Write an HTTP server that responds to JSON data every time it receives a GET request with the path ‘/ API /parsetime’. We expect the request to contain a query parameter (Query String) with the key being “ISO” and the value being the ISO time.
- url:
/api/parsetime? Iso = 2017-04-05 T12:10:15. 474 z
The JSON response should contain only three attributes: ‘hour’, ‘minute’, and ‘second’. Such as:
{
"hour":21."minute":45."second":30
}
Copy the code
- url:
/api/unixtime? Iso = 2017-04-05 T12:10:15. 474 z
, its return will contain an attribute: ‘unixtime’, the corresponding value is a UNIX timestamp. Such as:
{ "unixtime": 1376136615474 }
Copy the code
The actual code might look like this:
const http = require('http')
const url = require('url')
const hostname = '127.0.0.1'
const port = 3000
/ * * *@desc: Parsing time *@param {Object} Time, date object *@return {Object}* /
function parsetime(time) {
return {
hour: time.getHours(),
minute: time.getMinutes(),
second: time.getSeconds()
}
}
/ * * *@desc: Unix time *@return {Object}* /
function unixtime(time) {
return { unixtime: time.getTime() }
}
const server = http.createServer((req, res) = > {
let parsedUrl = url.parse(req.url, true)
let time = new Date(parsedUrl.query.iso)
let result
// Home page, return the current time json
if(req.url=='/'){
result = parsetime(new Date()}// Return json of the query time
else if (/^\/api\/parsetime/.test(req.url)) {
result = parsetime(time)
}
// Return unixtime of the query time
else if (/^\/api\/unixtime/.test(req.url)) {
result = unixtime(time)
}
if (result) {
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(result))
} else {
res.writeHead(404)
res.end()
}
})
server.listen(port, hostname, () = > {
console.log(The server runs in http://${hostname}:${port}`)})Copy the code
The Http client
In Node.js, you can easily request data from other sites using the Request method or http.get(options[, callback]).
http.request(options, callback)
The options argument to the request method can be either an object or a string. If it’s a string, it means it’s a URL, and Node automatically calls url.parse() to handle this argument.
Http.request () returns an instance of the http.ClientRequest class. It is a writable data stream, and if you want to send a file via POST, you can write the file to this ClientRequest object
Now let’s try www.github.com
const http = require('http')
let options = {
hostname: 'www.example.com'.port: 80.path: '/'.method: 'GET'
}
const req = http.request(options, (res) = > {
console.log(`STATUS: ${res.statusCode}`) // Return the status code
console.log(`HEADERS: The ${JSON.stringify(res.headers, null.4)}`) // return to the head
res.setEncoding('utf8') // Set the encoding
res.on('data'.(chunk) = > { // Listen for the 'data' event
console.log(` subject:${chunk}`)
})
})
req.end() // the end method ends the request
Copy the code
Here we request the information on the website, based on which we can develop more useful crawlers and extract useful information. If you are interested, you can also study further.
Commonly used NodeJS NPM package
- Express is a concise and flexible Node.js Web application framework that provides a series of powerful features to help you create a variety of Web applications, as well as rich HTTP tools. Chinese website
- Koa, koA is a new Web framework, built by the same people behind Express, that aims to be a smaller, more expressive, and more robust cornerstone of web application and API development. By making use of async functions, Koa helps you discard callback functions and greatly enhances error handling. Chinese website
- Request, the request module makes HTTP requests much simpler