Node.js:在同一目录下发送文件
我希望这个问题不是太可笑。
var app = require('express')(); var http = require('http').Server(app); app.get('/', function(req,res){ res.sendFile('index.html'); }); http.listen(3000,function(){ console.log('/','listening on *:3000'); });
我正在运行Linux mint,并且每当我尝试在本地运行时,我得到以下错误:
错误:ENOENT,stat'index.html'错误(本机)
我相信这跟目录有关。 index.html
文件与index.js
文件位于同一文件夹中。 我已经找遍了,在这种情况下找不到这个确切的错误,所以有点困惑。 我是否把错误的目录放在了
app.get('/', function(req,res){ res.sendFile('index.html'); });
你需要指定你的index.html文件的位置。 尝试使用下面的代码片段
var app = require('express')(); var http = require('http').Server(app); var path = require('path'); app.get('/', function(req,res){ res.sendFile(path.join(__dirname, 'index.html')); }); http.listen(3000,function(){ console.log('/','listening on *:3000'); });