node js 开发网站_使用Node JS开发网站

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.

必须具备JavaScriptHTML的基础知识。

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

Develop a website in Node Js | Output file 1

Output screenshot 2

输出屏幕截图2

Develop a website in Node Js | Output file 2

Output screenshot 3

输出屏幕截图3

Develop a website in Node Js | Output file 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 开发网站

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值