Node.js is a platform built on the Chrome JavaScript runtime.
Node.js is an event-driven I/O server-side JavaScript environment based on Google's V8 engine. The V8 engine executes Javascript very quickly and has very good performance.
Node.js web module syntax
Web server generally refers to a website server, which refers to a program that resides on a certain type of computer on the Internet. The basic function of a Web server is to provide Web information browsing services. It only needs to support the HTTP protocol, HTML document format and URL, and cooperate with the client's web browser.
Most web servers support server-side scripting languages ??(php, python, ruby), etc., and obtain data from the database through scripting languages ??and return the results to the client browser.
Node.js web module example
var http = require('http'); var fs = require('fs'); var url = require('url'); // 創(chuàng)建服務(wù)器http.createServer( function (request, response) { // 解析請(qǐng)求,包括文件名 var pathname = url.parse(request.url).pathname; // 輸出請(qǐng)求的文件名 console.log("Request for " + pathname + " received."); // 從文件系統(tǒng)中讀取請(qǐng)求的文件內(nèi)容 fs.readFile(pathname.substr(1), function (err, data) { if (err) { console.log(err); // HTTP 狀態(tài)碼: 404 : NOT FOUND // Content Type: text/plain response.writeHead(404, {'Content-Type': 'text/html'}); }else{ // HTTP 狀態(tài)碼: 200 : OK // Content Type: text/plain response.writeHead(200, {'Content-Type': 'text/html'}); // 響應(yīng)文件內(nèi)容 response.write(data.toString()); } // 發(fā)送響應(yīng)數(shù)據(jù) response.end(); }); }).listen(8080); // 控制臺(tái)會(huì)輸出以下信息console.log('Server running at http://127.0.0.1:8080/');