var http=require ("http");
Import file Module
var fs=require ("FS");
var server =http.createserver (function (req,res) {
Console printing Information
Console.log ("Server is working");
End server Response, note: There is no res.end (), the page will be loaded in the state when the page is opened
Res.end ("Server end.");
Set Header information
Res.setheader ("Content-type", "text/html;charset= ' Utf-8 '");
Content that is printed directly on the page
Res.write ("Response write content.");
Read the file
Fs. ReadFile ("./index.html", "Utf-8", function (Err,data) {
if (err) {
Console.log ("Index.html loading is failed:" +err);
}
else{
Back to Index.html page
Res.end (data);
}
});
Listening port, note: A program can only listen on one port
}). Listen (8888);
Index.html:
<! DOCTYPE html>
<meta charset= "UTF-8" >
<title></title>
<body>
Hello, welcome to the world of Nodejs, here is my homepage.
</body>
Nodejs Creating a service and loading an HTML file