我想Node.js的阅读form.html时域名为localhost:3000 /形式,但由于某些原因,它总是给我一个错误页500。Node.js的fs.ReadFile总是返回错误
在fs.readFile的回调函数的内容参数会不确定,即使该文件的路径是正确的。
app.get('/form', function(req, res){
fs.readFile('/form.html', function(error, content){
if(error){
// This get's always executed... I don't know why.
// content = undefined.
res.writeHead(500);
res.end();
}
else{
res.writeHead(200, { 'content-type' : 'text/html' });
processFile(content);
res.end(content, 'utf-8');
}
});
});
添加错误消息:
{ [Error: ENOENT, open 'C:\form.html'] errno: 34, code: 'ENOENT', path: 'C:\form.html' }
我必须指定文件的完整路径...?
我删除了之后/我得到这样的路径:
C:\Users\deno_000\form.html
我的文件都在同一目录下,并在我的编辑器的左侧,你可以看到它:
如果您记录错误并在此处发布错误消息,这将有所帮助。 – prattsj
也 - 只是一个猜测,但也许它是'./form.html',除非在根 – bryanmac
奥克我添加了错误信息,看起来像我必须指定完整的路径? –