Nodejs Creating a service and loading an HTML file

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.