Home >Web Front-end >JS Tutorial >Great node.js reading notes - node learning summary_node.js
The project work this week is relatively scattered (it should be said that it has always been like this), so the summary will be divided into two parts according to different situations~ This article records the learning summary about node, and the next article is about the web learned during the project Front-end knowledge.
1.HTTP
The HTTP module of node was exposed in the first article. Here we will learn the APIs that appear in several routines.
require('http').createServer(function(req, res){
If('/' == req.url){
res.writeHead(200, {'Content-Type': 'text/html'});
res.end([
'
The parameter of the creatServer([requestListener]) function is a callback function function(req, res), where req (request) is an instance of http.IncomingMessage, and res (response) is an instance of http.ServerRrsponse.
We used the res url, method string and two methods writeHead and end. As the name suggests, url is the URL that records HTTP (everything after the host name), and method is the method for recording HTTP responses.
writeHead(statusCode, [reasonPhrase], [headers]) is used to send an http response header information. This method is only called once when the message arrives, and must be called before the end method. If you do this instead and call the write(chunk, [encoding]) or end([data], [encoding]) method first, the system will automatically record an invisible and volatile (in short, bad) response header content. And call the writeHead method.
The end method will send a message to the server to indicate that the response information has been sent, so this method must be called every time the response is sent. When its parameter has content (such as a routine), this method is equivalent to calling the write('content', [encoding]) and end methods at the same time. This is quite convenient.
Next, the routine uses req.on to listen for events and bind it to req(message). Its prototype is Emitter.on(event, listener), req is the object that generates the event, and in the listening function, this points to the EventEmitter object associated with the current listening function.