node js 开发网站
You will have your own fully functional website running on "localhost" after going through this article.
阅读完本文后,您将在“ localhost”上运行自己的功能齐全的网站 。
Basic knowledge of JavaScript and HTML is a prerequisite.
必须具备JavaScript和HTML的基础知识。
Here are the source codes of the webpages...
这是网页的源代码...
JS file (a.js)
JS档案(a.js)
var http = require('http');
var fs = require('fs');
function notfoundfunc(response) {
response.writeHead(404, {
"Context-Type": "text/plain"
});
response.write("page not found");
response.end();
}
function myfunc(request, response) {
if (request.method == 'GET' && request.url == '/') {
response.writeHead(200, {
"Context-Type": "text/html"
});
fs.createReadStream("./index.html").pipe(response);
} else if (request.method == 'GET' && request.url == '/about') {
response.writeHead(200, {
"Context-Type": "text/html"
});
fs.createReadStream("./about.html").pipe(response);
} else {
notfoundfunc(response);
}
}
http.createServer(myfunc).listen(3500);
console.log("server made");
index.html
index.html
<html>
<body>
welcome to my website.
</body>
</html>
about.html
about.html
<html>
<body>
developed by mansha lamba.
</body>
</html>
Output screenshot 1
输出屏幕截图1
Output screenshot 2
输出屏幕截图2
Output screenshot 3
输出屏幕截图3
Explanation of the code:
代码说明:
The source code is very easy to understand.
源代码非常容易理解。
Please note that instead of giving a plain text output like in my previous article here I have used HTML files. For that, another inbuilt module in NODE JS is used i.e. fs.
请注意,不是使用纯文本输出,而是使用HTML文件。 为此,使用了NODE JS中的另一个内置模块,即fs。
Rest of the code is self-explanatory.
其余代码是不言自明的。
翻译自: https://www.includehelp.com/node-js/developing-a-website.aspx
node js 开发网站