使用node.js作为简单的Web服务器

node.js Node.js

小小Stafan宝儿

2020-03-13

我想运行一个非常简单的HTTP服务器。每个GET请求example.com都应index.html以常规HTML页面的形式(例如,与您阅读普通网页时相同的体验)获得响应

使用下面的代码,我可以阅读的内容index.html如何index.html作为常规网页?

var http = require('http');
var fs = require('fs');
var index = fs.readFileSync('index.html');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end(index);
}).listen(9615);

下面的一个建议很复杂,需要我为get要使用的每个资源(CSS,JavaScript,图像)文件一行。

如何在单个HTML页面中提供一些图像,CSS和JavaScript?

第1499篇《使用node.js作为简单的Web服务器》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

10个回答
猪猪JinJin西里 2020.03.13

This is one of the fastest solutions i use to quickly see web pages

sudo npm install ripple-emulator -g

From then on just enter the directory of your html files and run

ripple emulate

then change the device to Nexus 7 landscape.

LEYMandy 2020.03.13

There are already some great solutions for a simple nodejs server. There is a one more solution if you need live-reloading as you made changes to your files.

npm install lite-server -g

navigate your directory and do

lite-server

it will open browser for you with live-reloading.

小小古一 2020.03.13

你可以在shell中输入

npx serve

回购:https : //github.com/zeit/serve

阿飞乐 2020.03.13

我这样做的方法是首先通过以下方式全局安装节点静态服务器

npm install node-static -g

然后导航到包含html文件的目录,并使用启动静态服务器static

转到浏览器并输入localhost:8080/"yourHtmlFile"

阿飞宝儿猴子 2020.03.13

您无需使用任何NPM模块即可运行简单的服务器,Node的库非常小,名为“ NPM Free Server ”:

50行代码,如果您请求文件或文件夹,则输出,如果工作失败,则将其显示为红色或绿色。小于1KB(最小)。

宝儿小小 2020.03.13

如果您的PC上安装了节点,则可能是NPM;如果不需要NodeJS,则可以使用serve软件包:

1-在您的PC上安装软件包:

npm install -g serve

2-服务您的静态文件夹:

serve <path> 
d:> serve d:\StaticSite

它将显示您要为您的静态文件夹提供服务的端口,只需导航至主机,如:

http://localhost:3000
An 2020.03.13

编辑:

Node.js示例应用程序Node Chat具有您想要的功能。
在它的README.textfile
3中。步骤就是您要寻找的。

步骤1

  • 创建一个在端口8002上以hello world响应的服务器

第2步

  • 创建一个index.html并将其投放

第三步

  • 介绍util.js
  • 更改逻辑,以便提供任何静态文件
  • 在找不到文件的情况下显示404

步骤4

  • 添加jquery-1.4.2.js
  • 添加client.js
  • 更改index.html以提示用户输入昵称

这是server.js

这是util.js

泡芙 2020.03.13

我在npm上找到了一个有趣的库,可能对您有用。它称为mime(npm install mimehttps://github.com/broofa/node-mime),它可以确定文件的mime类型。这是我使用它编写的Web服务器的示例:

var mime = require("mime"),http = require("http"),fs = require("fs");
http.createServer(function (req, resp) {
path  = unescape(__dirname + req.url)
var code = 200
 if(fs.existsSync(path)) {
    if(fs.lstatSync(path).isDirectory()) {
        if(fs.existsSync(path+"index.html")) {
        path += "index.html"
        } else {
            code = 403
            resp.writeHead(code, {"Content-Type": "text/plain"});
            resp.end(code+" "+http.STATUS_CODES[code]+" "+req.url);
        }
    }
    resp.writeHead(code, {"Content-Type": mime.lookup(path)})
    fs.readFile(path, function (e, r) {
    resp.end(r);

})
} else {
    code = 404
    resp.writeHead(code, {"Content-Type":"text/plain"});
    resp.end(code+" "+http.STATUS_CODES[code]+" "+req.url);
}
console.log("GET "+code+" "+http.STATUS_CODES[code]+" "+req.url)
}).listen(9000,"localhost");
console.log("Listening at http://localhost:9000")

这将服务于任何常规文本或图像文件(.html,.css,.js,.pdf,.jpg,.png,.m4a和.mp3是我测试过的扩展名,但从理论上讲,它应该适用于所有内容)

开发者须知

这是我得到的输出示例:

Listening at http://localhost:9000
GET 200 OK /cloud
GET 404 Not Found /cloud/favicon.ico
GET 200 OK /cloud/icon.png
GET 200 OK /
GET 200 OK /501.png
GET 200 OK /cloud/manifest.json
GET 200 OK /config.log
GET 200 OK /export1.png
GET 200 OK /Chrome3DGlasses.pdf
GET 200 OK /cloud
GET 200 OK /-1
GET 200 OK /Delta-Vs_for_inner_Solar_System.svg

注意unescape路径构造中功能。这是为了允许文件名带有空格和编码字符。

小胖Sam 2020.03.13

步骤1(在命令提示符内[希望您CD到文件夹]): npm install express

步骤2:建立档案server.js

var fs = require("fs");
var host = "127.0.0.1";
var port = 1337;
var express = require("express");

var app = express();
app.use(express.static(__dirname + "/public")); //use static files in ROOT/public folder

app.get("/", function(request, response){ //root dir
    response.send("Hello!!");
});

app.listen(port, host);

Please note, you should add WATCHFILE (or use nodemon) too. Above code is only for a simple connection server.

STEP 3: node server.js or nodemon server.js

There is now more easy method if you just want host simple HTTP server. npm install -g http-server

and open our directory and type http-server

https://www.npmjs.org/package/http-server

Me无敌 2020.03.13

我认为您现在缺少的部分是您要发送的内容:

Content-Type: text/plain

如果要使用Web浏览器呈现HTML,则应将其更改为:

Content-Type: text/html

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android