JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

12.2.1 Node Example: HTTP Server

12.2.1 Node Example: HTTP Server


Example 12-2 is a simple HTTP server in Node. It serves files from the current directory and also implements two special-purpose URLs that it handles specially. It uses Node’s “http” module and also uses the file and stream APIs demonstrated earlier. Exam ple 18-17 in Chapter 18 is a similar specialized HTTP server example.

Example 12-2. An HTTP server in Node

// This is a simple NodeJS HTTP server that can serve files from the current // directory and also implements two special URLs for testing. // Connect to the server at http://localhost:8000 or http://127.0.0.1:8000

// First, load the modules we'll be using var http = require('http'); // HTTP server API var fs = require('fs'); // For working with local files

var server = new http.Server(); // Create a new HTTP server server.listen(8000); // Run it on port 8000.

// Node uses the "on()" method to register event handlers. // When the server gets a new request, run this function to handle it. server.on("request", function (request, response) {

// Parse the requested URL var url = require('url').parse(request.url);

// A special URL that just makes the server wait before sending the // response. This can be useful to simulate a slow network connection. if (url.pathname === "/test/delay") {

// Use query string for delay amount, or 2000 milliseconds var delay = parseInt(url.query) || 2000; // Set the response status code and headers response.writeHead(200, {"Content-Type": "text/plain; charset=UTF-8"}); // Start writing the response body right away response.write("Sleeping for " + delay + " milliseconds..."); // And then finish it in another function invoked later. setTimeout(function() {

response.write("done."); response.end();

}, delay); } // If the request was for "/test/mirror", send back the request verbatim. // Useful when you need to see the request headers and body. else if (url.pathname === "/test/mirror") {

// Response status and headers response.writeHead(200, {"Content-Type": "text/plain; charset=UTF-8"}); // Begin the response body with the request response.write(request.method + " " + request.url +

" HTTP/" + request.httpVersion + "\r\n"); // And the request headers for(var h in request.headers) {

response.write(h + ": " + request.headers[h] + "\r\n"); } response.write("\r\n"); // End headers with an extra blank line

// We complete the response in these event handler functions: // When a chunk of the request body, add it to the response. request.on("data", function(chunk) { response.write(chunk); }); // When the request ends, the response is done, too. request.on("end", function(chunk) { response.end(); });

} // Otherwise, serve a file from the local directory. else {

// Get local filename and guess its content type based on its extension. var filename = url.pathname.substring(1); // strip leading / var type; switch(filename.substring(filename.lastIndexOf(".")+1)) { // extension case "html": case "htm": type = "text/html; charset=UTF-8"; break; case "js": type = "application/javascript; charset=UTF-8"; break;

12.2 Asynchronous I/O with Node | 301

case "css": type = "text/css; charset=UTF-8"; break; case "txt" : type = "text/plain; charset=UTF-8"; break; case "manifest": type = "text/cache-manifest; charset=UTF-8"; break; default: type = "application/octet-stream"; break; }

// Read the file asynchronously and pass the content as a single // chunk to the callback function. For really large files, using the // streaming API with fs.createReadStream() would be better. fs.readFile(filename, function(err, content) {

if (err) { // If we couldn't read the file for some reason response.writeHead(404, { // Send a 404 Not Found status

"Content-Type": "text/plain; charset=UTF-8"}); response.write(err.message); // Simple error message body response.end(); // Done

} else { // Otherwise, if the file was read successfully. response.writeHead(200, // Set the status code and MIME type {"Content-Type": type}); response.write(content); // Send file contents as response body

response.end(); // And we're done
}
});
}
});
欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/31405.html
友情链接It题库(ittiku.com)| 版权归yishouce.com所有| 友链等可联系 admin#yishouce.com|粤ICP备16001685号-1