I want to run a very simple HTTP server.
(我想运行一个非常简单的HTTP服务器。)
Every GET request to example.com
should get index.html
served to it but as a regular HTML page (ie, same experience as when you read normal web pages). (对example.com
每个GET请求都应该将index.html
提供给它,但是作为常规HTML页面(即,与阅读普通网页时相同的体验)。)
Using the code below, I can read the content of index.html
.
(使用下面的代码,我可以阅读index.html
的内容。)
How do I serve index.html
as a regular web page? (如何将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);
One suggestion below is complicated and requires me to write a get
line for each resource (CSS, JavaScript, images) file I want to use.
(下面的一个建议很复杂,需要我为我想要使用的每个资源(CSS,JavaScript,图像)文件写一个get
行。)
How can I serve a single HTML page with some images, CSS and JavaScript?
(如何使用一些图像,CSS和JavaScript提供单个HTML页面?)
ask by idophir translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…