Linux uses node. js to establish a service instance that accesses static Web pages
First, install the environment that node. js needs to run.
Second, create the node directory (/NODE/WWW) and create the node. JS Service file under the directory Server.js
var http = require (' http ');
var fs = require (' FS ');//introduction of file reading module
var documentroot = '/node/www ';//directory of files to be accessed
var server= http.createserver (function (req,res) {
var url = req.url;
The URL entered by the client, for example, if the input localhost:9999/index.html
So here's the URL = =/index.html
var file = documentroot + URL;
Console.log (URL);//node/www/index.html
/*
File path
function is a callback,
The Err of function is the information returned by the read error, and there is no error when returning NULL
function data for reading the text content returned successfully
*/
Fs.readfile (file, function (Err,data) {
if (err) {
Res.writeheader (404,{
' Content-type ': ' text/html;charset= ' utf-8 '
});
Res.write (' Res.end ();
}else{
Res.writeheader (200,{
' Content-type ': ' text/html;charset= ' utf-8 '
});
Res.write (data);//display index.html on the client
Res.end ();
}
});
}). Listen (9999);
Console.log (' Server open successfully ... ');
Third, create index.html home file, put the path to/node/www/below
Four, start the Service command: node Server.js
Five, browser input address: http://localhost:9999/index.html
Linux uses node. js to establish a service instance that accesses static Web pages