Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 节点静态示例_Node.js_Node Static - Fatal编程技术网

Node.js 节点静态示例

Node.js 节点静态示例,node.js,node-static,Node.js,Node Static,我一直在绞尽脑汁想弄清楚这个代码到底出了什么问题。我直接从节点静态github存储库示例复制了它,但它似乎不起作用。我所做的唯一修改是对公共文件的路径(以前是“/public”)。在我的公用文件夹中,我有一个index.html,但是当我点击http://localhost:8080/index.html我什么也得不到 var static = require('node-static'); // // Create a node-static server instance to serve

我一直在绞尽脑汁想弄清楚这个代码到底出了什么问题。我直接从节点静态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);
如果重要的话,我将在64位Windows 7上运行Node.js

编辑:


我加入了一些
console.log
语句,它进入了服务器处理程序,但没有进入侦听器处理程序。这可能与“结束”事件有关吗?

我认为您有windows路径问题。我无法在我这方面进行验证,但我可以给您两个选项:

1) 避开你的反斜杠:

'C:\\Projects\\node\\public'
2) 使用


\uu dirname
是当前文件的路径。

我删除了
请求。addListener('end',…)
函数:

require('http').createServer(function (request, response) {

    //
    // Serve files!
    //
    file.serve(request, response);

}).listen(8080);

现在它工作正常。

此外,您还可以提供一个错误页面:

let static=require('node-static');
让fileServer=newstatic.Server('C:\Projects\node\public');
require('http').createServer(函数(请求、响应){
服务(请求、响应、函数(异常){
如果(异常&(异常状态===404)){
serveFile('/404.html',404,{},请求,响应);
}
});
}).听(8080);

文件
404.html
应该存在

我试过了,但是没有用。我还编辑了原始问题以包含更多细节。使用express的其他解决方案:
app.use(express.static(“../client”).resume()
添加到处理程序绑定的末尾,如
request.addListener(…).resume()
。它将强制节点触发结束事件。
require('http').createServer(function (request, response) {

    //
    // Serve files!
    //
    file.serve(request, response);

}).listen(8080);