nodeJS:使用(fs)尝试访问静态资源,但收到404错误
nodeJS: using (fs) to try and access a static resource but receive 404 error?
我正试图使用指向静态资源的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-dep-pago.html文件在avisoPago文件夹中。
因此,index.js的路径是:var/www/html/avisoPago/index.js
avisos-de-pago.html文件的路径为:var/www/html/avisoPago/aviso-de-pago.html
我在做什么,当我键入时它找不到文件http://myDomain:3000/avisarPagoAhora
var path = req.url.toLowerCase();
"toLowerCase"函数使"avisarPagoAhora"变成"avisarpagahora",因此节点无法找到正确的路径'/avisarPagoAhora'
。尝试删除toLowerCase函数或使case语句"case '/avisarpagoahora':
"
相关文章:
- 用于查询错误转换的角度资源返回列表
- "资源不足错误“;当从Chrome控制台重复发出ajax请求时
- 无法加载Angular js中的资源错误
- 否'访问控制允许来源'当跨域设置为true时,请求的资源错误中存在标头
- angular未能加载检查器中的资源错误
- Azure Blob 存储 请求的资源错误中不存在访问控制允许源标头
- 如何在 MSSQL 中调试资源 #6 错误
- 捕获由
中的 HTML 标记导致的“无法加载资源”错误 - 跨源资源错误:请求的资源上不存在“访问控制允许源”标头
- HTML5 视频 - 开始播放后无法加载资源错误
- 如何通过 JavaScript 列出 404 资源错误
- Kibana:无法加载资源 |错误
- 重构angular ng资源错误处理
- Angularjs资源错误:a.push不是一个函数
- Javascript语句不工作…获取'加载资源'错误
- 不允许加载本地资源错误
- 使用css2stylus后,手写笔文件无法加载资源错误
- 在资源错误回调的范围内访问表单
- 无法加载javascript中ajax调用的资源错误
- 加载外部脚本时无法加载资源错误