Home >Web Front-end >JS Tutorial >Great node.js reading notes - node learning summary_node.js

Great node.js reading notes - node learning summary_node.js

WBOY
WBOYOriginal
2016-05-16 16:25:091508browse

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.

Copy code The code is as follows:

var qs = require('querystring');

require('http').createServer(function(req, res){
If('/' == req.url){
          res.writeHead(200, {'Content-Type': 'text/html'});
res.end([
              '

',
'

My form

',
               '
',
                 '',
'

What is your name?

',
              '',
'

',
              '',
].join(''));
}else if('/url' == req.url && 'POST' == req.method){
        var body = '';
          req.on('data', function(chunk){
               body = chunk;
        });
          req.on('end', function(){
               res.writeHead(200, {'Content-Type': 'text/html'});
                res.end('Your name is ' qs.parse(body).name '

');
        });
}else{
           res.writeHead(404);
          res.end('not found');
}
}).listen(3000);

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn