使用 node.js 显示 html 页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15231864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
display html page with node.js
提问by user2137087
This is my first time with node.js. I get it to display the index.html, but it doesn't display the images on the site or anything else, it ONLY shows the basic html stuff. Here's how I set it up. There's no apache, php or anything else on the server, just ubuntu, proftp and node(and curl and the other dependencies). I made the main directory for the node files /var/nodeFiles and the directory for the html/site files is /var/nodeFiles/www so for my node server file I did it like this:
这是我第一次使用 node.js。我让它显示 index.html,但它不显示网站上的图像或其他任何东西,它只显示基本的 html 内容。这是我如何设置的。服务器上没有 apache、php 或其他任何东西,只有 ubuntu、proftp 和 node(以及 curl 和其他依赖项)。我为节点文件 /var/nodeFiles 创建了主目录,而 html/site 文件的目录是 /var/nodeFiles/www 所以对于我的节点服务器文件,我是这样做的:
var http = require('http'),
fs = require('fs');
fs.readFile('/var/nodeFiles/www/index.html', function (err, html) {
if (err) {
throw err;
}
http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}).listen(80);
});
this works, but it ONLY shows the index.html file and NOTHING attached to it, so no images, no effects or anything that the html file should display. The files and directories are all correct, I've double checked and the permissions of the folders are correct. So what else do I have to do to get node to display the rest of the site? I hope I've explained my self correctly, I was told this is the place to ask development questions. Thank you for taking the time to read this.
这有效,但它只显示 index.html 文件并且没有任何附加内容,因此没有图像,没有效果或 html 文件应显示的任何内容。文件和目录都是正确的,我已经仔细检查过,文件夹的权限是正确的。那么我还需要做什么才能让节点显示站点的其余部分?我希望我已经正确地解释了我自己,有人告诉我这是提出开发问题的地方。感谢您抽出时间来阅读。
回答by Hector Correa
but it ONLY shows the index.html file and NOTHING attached to it, so no images, no effects or anything that the html file should display.
但它只显示 index.html 文件,没有附加任何内容,因此没有图像,没有效果或 html 文件应显示的任何内容。
That's because in your program that's the only thing that you return to the browser regardless of what the request looks like.
那是因为在您的程序中,无论请求是什么样的,您都会返回浏览器的唯一内容。
You can take a look at a more complete example that will return the correct files for the most common web pages (HTML, JPG, CSS, JS) in here https://gist.github.com/hectorcorrea/2573391
您可以在此处查看一个更完整的示例,该示例将为最常见的网页(HTML、JPG、CSS、JS)返回正确的文件https://gist.github.com/hectorcorrea/2573391
Also, take a look at this blog post that I wrote on how to get started with node. I think it might clarify a few things for you: http://hectorcorrea.com/blog/introduction-to-node-js
另外,看看我写的这篇关于如何开始使用 node.js 的博客文章。我认为它可能会为您澄清一些事情:http: //hectorcorrea.com/blog/introduction-to-node-js
回答by Tsimtsum
Check this basic code to setup html server. its work for me.
检查此基本代码以设置 html 服务器。它对我来说有效。
var http = require('http'),
fs = require('fs');
fs.readFile('./index.html', function (err, html) {
if (err) {
throw err;
}
http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}).listen(8000);
});
回答by Coded Container
This did the trick for me:
这对我有用:
var express = require('express'),
app = express();
app.use('/', express.static(__dirname + '/'));
app.listen(8080);
回答by onaclov2000
If your goal is to simply display some static files you can use the Connect package. I have had some success (I'm still pretty new to NodeJS myself), using it and the twitter bootstrap API in combination.
如果您的目标是简单地显示一些静态文件,您可以使用 Connect 包。我已经取得了一些成功(我自己对 NodeJS 还是很陌生),结合使用它和 twitter bootstrap API。
at the command line
在命令行
:\> cd <path you wish your server to reside>
:\> npm install connect
Then in a file (I named) Server.js
然后在一个文件(我命名)Server.js
var connect = require('connect'),
http = require('http');
connect()
.use(connect.static('<pathyouwishtoserve>'))
.use(connect.directory('<pathyouwishtoserve>'))
.listen(8080);
Finally
最后
:\>node Server.js
Caveats:
注意事项:
If you don't want to display the directory contents, exclude the .use(connect.directory line.
如果您不想显示目录内容,请排除 .use(connect.directory 行。
So I created a folder called "server" placed index.html in the folder and the bootstrap API in the same folder. Then when you access the computers IP:8080 it's automagically going to use the index.html file.
所以我创建了一个名为“server”的文件夹,将 index.html 和 bootstrap API 放在同一个文件夹中。然后当您访问计算机 IP:8080 时,它会自动使用 index.html 文件。
If you want to use port 80 (so just going to http://, and you don't have to type in :8080 or some other port). you'll need to start node with sudo, I'm not sure of the security implications but if you're just using it for an internal network, I don't personally think it's a big deal. Exposing to the outside world is another story.
如果您想使用端口 80(因此只需访问 http://,而不必输入 :8080 或其他端口)。你需要用 sudo 启动节点,我不确定安全隐患,但如果你只是将它用于内部网络,我个人认为这没什么大不了的。与外界接触是另一回事。
Update 1/28/2014:
2014 年 1 月 28 日更新:
I haven't had to do the following on my latest versions of things, so try it out like above first, if it doesn't work (and you read the errors complaining it can't find nodejs), go ahead and possibly try the below.
我不必对我的最新版本执行以下操作,因此请先像上面一样尝试,如果它不起作用(并且您阅读了抱怨它找不到 nodejs 的错误),请继续并可能尝试下面。
End Update
结束更新
Additionally when running in ubuntu I ran into a problem using nodejs as the name (with NPM), if you're having this problem, I recommend using an alias or something to "rename" nodejs to node.
此外,在 ubuntu 中运行时,我遇到了使用 nodejs 作为名称(使用 NPM)的问题,如果您遇到此问题,我建议使用别名或其他名称将 nodejs“重命名”为 node。
Commands I used (for better or worse):
我使用的命令(无论好坏):
Create a new file called node
创建一个名为 node 的新文件
:\>gedit /usr/local/bin/node
#!/bin/bash
exec /nodejs "$@"
sudo chmod -x /usr/local/bin/node
That ought to make
那应该使
node Server.js
work just fine
工作得很好

