繁体   English   中英

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

[英]nodeJS: using (fs) to try and access a static resource but receive 404 error?

我正在尝试使用nodeJS生成一个指向静态资源的URL,这是一个JS文件。 它抛出了404错误,Not Found。

首先,我用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

当我输入http:// myDomain:3000 / avisarPagoAhora时,我在做什么,它找不到文件

var path = req.url.toLowerCase(); “toLowerCase”功能使“avisarPagoAhora”变成“avisarpagoahora”,因为它节点找不到正确的路径'/avisarPagoAhora' 尝试删除toLowerCase函数或使case语句“ case '/avisarpagoahora':

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM