Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.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
Javascript nodeJS:使用(fs)尝试访问静态资源,但收到404错误?_Javascript_Node.js_Path_Fs - Fatal编程技术网

Javascript nodeJS:使用(fs)尝试访问静态资源,但收到404错误?

Javascript nodeJS:使用(fs)尝试访问静态资源,但收到404错误?,javascript,node.js,path,fs,Javascript,Node.js,Path,Fs,我试图使用nodeJS生成一个指向静态资源的url,这是一个JS文件。它抛出了一个404错误,找不到 首先,我使用node index.js node.js文件包含以下内容: var fs = require('fs'); function serveStaticFile(res, path, contentType, responseCode) { if(!responseCode) responseCode = 200; fs.readFile(__dirname + pa

我试图使用nodeJS生成一个指向静态资源的url,这是一个JS文件。它抛出了一个404错误,找不到

首先,我使用
node index.js

node.js文件包含以下内容:

var fs = require('fs');

function serveStaticFile(res, path, contentType, responseCode) {
    if(!responseCode) responseCode = 200;
    fs.readFile(__dirname + path, function(err,data){
        if(err){
            res.writeHead(500,{'Content-Type': 'text/plain'});
            res.end('500 - Internal Error');
        } else {
            res.writeHead(responseCode,
                    {'Content-Type': 'text/plain'});
            res.end(data);
        }
    });
}

http.createServer(function(req,res){

    var path = req.url.toLowerCase();
    switch(path) {
        case '/avisarPagoAhora':
                serveStaticFile(res, '/avisoPago/avisos-de-pago.html', 'text/html');
                break;
        default:
                res.writeHead(404,{'Content-Type': 'text/plain'});
                res.end('Not Found');
                break;      
    }


}).listen(3000);

console.log('Server started on localhost:3000; press Ctrl-C to terminate...');
现在,我在var/www/html/avisoPago中找到了index.js

我的avisos-de-pago.html文件位于avisoPago中的avisoPago文件夹中

因此,index.js的路径是:var/www/html/avisoPago/index.js

而avisos-de-pago.html文件的路径是:var/www/html/avisoPago/avisoPago/avisos de pago.html


当我键入
var path=req.url.toLowerCase()时,它找不到文件,我在做什么
函数“toLowerCase”使“avisarpagoahera”变为“avisarpagoahera”,因此节点无法找到正确的路径
'/avisarpagoahera'
。尝试删除toLowerCase函数或使用case语句“
case'/avisarpagoahora':

执行
console.log(u dirname+path)
以查看它实际查找的文件。