我刚刚在本地计算机上使用 socket.io 设置了一个基本的 Node.js 服务器。有没有办法设置文档根目录以便可以包含其他文件。 IE。下面我有一个带有背景图像的 DIV。图像的路径相对于服务器的位置,但这不起作用。有任何想法吗?谢谢!
var http = require('http'),
io = require('socket.io'), // for npm, otherwise use require('./path/to/socket.io')
server = http.createServer(function(req, res){
// your normal server code
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<div style="background-image:url(img/carbon_fibre.gif);"><h1>Hello world</h1></div>');
});
server.listen(8080);
// socket.io
var socket = io.listen(server);
最佳答案
使用Express或Connect 。示例:https://github.com/spadin/simple-express-static-server , http://senchalabs.github.com/connect/middleware-static.html
关于javascript - 将文档根目录设置为 Node.js http 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5822615/