Use node. js to build servers and node. js to build servers
Use node to build a small server (in fact, it is to analyze the url and then output the file to the client)
Recently, you need to complete a course design, which is assigned by the Project Manager (team lead) to write the interface. However, you always feel that there is something missing when writing only the front end, so you want to write down the back end for a bit.
During this period, I had a little bit about the language I used. I was planning to learn about PHP. But I thought about it later. It was not so nice to use nodejs, but I also learned about background development, it is also equivalent to consolidating the js Foundation.
In the process of learning node, I learned how to use node to implement a server. It is a good summary of the previous modules. Four basic modules are used: fs stream http path
The Code is as follows: (sorry for the bad English comments)
'Use strict 'var url = require ('url'); var path = require ('path'); var fs = require ('fs '); var http = require ('http'); // get the current path // var root = path. resolve ('. '); // use the current directory as the root directory of the server var root = path. resolve (process. argv [2] | '. '); // use the input parameters as the root directory of the server. If no parameters are input, the current directory is used as the root directory of the server console. log ('local root dir: '+ root); // create servervar server = http. createServer (function (request, response) {// get the path Of URL var pathname = url. parse (request. url ). pathname; // get the local path var filepath = path. join (root, pathname); // get the file stat and output the request file by callback function fs. stat (filepath, function (err, stat) {if (! Err & stat. isFile () {console. log ('20140901' + request. url); response. writehead( 200); fs. createReadStream (filepath ). pipe (response); // There is no need to manually read the file content. Because the response object itself is a Writable Stream, the pipe () method is used to automatically read the file content and output it to the HTTP response.} Else {console. log ('20140901' + request. url); response. writeHead (404); response. end ('2014 Not Found ') ;}}) ;}); server. listen (8080); console. log ('server is running at http: // 127.0.0.1: 8080 /');
Some functions are described as follows:
Path. resolve () path (this name is good) path. resolve ([from…], To)
There is an interesting explanation: It is equivalent to constantly calling the cd command of the system.
Eg:
Path. resolve ('foo/bar', '/tmp/file /','.. ', 'A /.. /subfile') // equivalent to: cd foo/barcd/tmp/file/cd .. cd /.. /subfile1path. join ([path1], path [2]...) merge paths
Concatenate all names with path. seq and format them with normailze
Eg:
path.join('///foo', 'bar', '//baz/asdf', 'quux', '..');=>'/foo/bar/baz/asdf'
Since normalize is mentioned
So:
Format path. normalize (p)
Formats non-compliant paths to simplify developers' processing of various complex path judgments.
Eg:
path.normalize('/foo/bar//baz/asdf/quux/..');=> '/foo/bar/baz/asdf'
Http. response. end () ends accordingly, telling the client that all messages have been sent. When all the content to be returned is sent, the function must be called once. If you do not call this function, the client will always be in the waiting state.
Usage:
response.end([data], [encoding])
The character to be output after data end () is executed. If the value of data is specified, it means the response is executed. after end (), a response is executed. write (data, encoding );
Character encoding of data corresponding to encoding
The above section describes how to use node. the js server construction method is helpful to you. If you have any questions, please leave a message. The editor will reply to you in time, thank you very much for your support for the help House website!