我一直在撞墙,试图弄清楚这段代码有什么问题.我直接从node-static github存储库示例复制它,但它似乎不起作用.我做的唯一修改是公共文件的路径(以前是'./public').在我的公共文件夹中,我有一个index.html,但是当我点击时,http://localhost:8080/index.html我什么也得不到.
var static = require('node-static');
//
// Create a node-static server instance to serve the './public' folder
//
var file = new(static.Server)('C:\Projects\node\public');
require('http').createServer(function (request, response) {
request.addListener('end', function () {
//
// Serve files!
//
file.serve(request, response);
});
}).listen(8080);
Run Code Online (Sandbox Code Playgroud)
如果重要的话,我在Windows 7 64位上运行Node.js.
编辑:
我抛出了一些console.log语句,它使它进入服务器处理程序,但不进入侦听器处理程序.这可能与'结束'事件有关吗?
我是node.js的新手.尝试在请求结束时让控制台进行打印.我尝试去localhost:8080和localhost:8080 /但终端没有打印.知道为什么吗?这样做是因为当我运行这个例子时,因为当我尝试在http://tutorialzine.com/2012/08/nodejs-drawing-game/上运行演示时,终端说套接字已启动,但它没有呈现index.html页面.所以我无法弄清楚为什么这个为其他人提供静态文件的代码对我不起作用.
var static = require('node-static');
//
// Create a node-static server instance to serve the './public' folder
//
// var file = new(static.Server)('./');
require('http').createServer(function (request, response) {
request.addListener('end', function () {
console.log("ended");
});
}).listen(8080);
Run Code Online (Sandbox Code Playgroud) 我不知道这是一个限制,node-static还是我的代码中的错误,但我似乎无法提供它来提供当前目录之上或之外的文件.我目前的目录结构是这样的:
project
public
...public stuff here...
system
core
server.js
Run Code Online (Sandbox Code Playgroud)
server.js生活在core目录中,使路径public为../../public- 但此代码将无法运行.它返回404.
staticServer = new (static.Server)('../../public');
webServer = http.createServer(function (request, response) {
staticServer.serve(request,response);
})
webServer.listen(appServerConfig.port, appServerConfig.address);
Run Code Online (Sandbox Code Playgroud)
但是,如果我更改结构以使公共文件夹位于旁边server.js并相应地更改代码,则它可以工作:
project
system
core
server.js
public
...public stuff here...
staticServer = new (static.Server)('./public');
webServer = http.createServer(function (request, response) {
staticServer.serve(request,response);
})
webServer.listen(appServerConfig.port, appServerConfig.address);
Run Code Online (Sandbox Code Playgroud)
为什么会这样?