admin管理员组文章数量:1417442
I am trying to create a contacts app with angularjs. I have created a file in the root directory of the project called server.js. Here is the code:
var express = require('express'),
app = express();
app
.use(express.static('./public'))
.get('*', function (req, res) {
res.sendfile('public/main.html');
})
.listen(3000);
When I go to localhost:3000, this is the error message that es up.
TypeError: path must be absolute or specify root to res.sendFile at ServerResponse.sendFile (D:\Workspace\contacts\node_modules\express\lib\response.js:389:11) at D:\Workspace\contacts\server.js:7:7 at Layer.handle [as handle_request] (D:\Workspace\contacts\node_modules\express\lib\router\layer.js:82:5) at next (D:\Workspace\contacts\node_modules\express\lib\router\route.js:100:13) at Route.dispatch (D:\Workspace\contacts\node_modules\express\lib\router\route.js:81:3) at Layer.handle [as handle_request] (D:\Workspace\contacts\node_modules\express\lib\router\layer.js:82:5) at D:\Workspace\contacts\node_modules\express\lib\router\index.js:235:24 at Function.proto.process_params (D:\Workspace\contacts\node_modules\express\lib\router\index.js:313:12) at D:\Workspace\contacts\node_modules\express\lib\router\index.js:229:12 at Function.match_layer (D:\Workspace\contacts\node_modules\express\lib\router\index.js:296:3)
Does anyone have any suggestions? Any help would be greatly appreciated.
I am trying to create a contacts app with angularjs. I have created a file in the root directory of the project called server.js. Here is the code:
var express = require('express'),
app = express();
app
.use(express.static('./public'))
.get('*', function (req, res) {
res.sendfile('public/main.html');
})
.listen(3000);
When I go to localhost:3000, this is the error message that es up.
TypeError: path must be absolute or specify root to res.sendFile at ServerResponse.sendFile (D:\Workspace\contacts\node_modules\express\lib\response.js:389:11) at D:\Workspace\contacts\server.js:7:7 at Layer.handle [as handle_request] (D:\Workspace\contacts\node_modules\express\lib\router\layer.js:82:5) at next (D:\Workspace\contacts\node_modules\express\lib\router\route.js:100:13) at Route.dispatch (D:\Workspace\contacts\node_modules\express\lib\router\route.js:81:3) at Layer.handle [as handle_request] (D:\Workspace\contacts\node_modules\express\lib\router\layer.js:82:5) at D:\Workspace\contacts\node_modules\express\lib\router\index.js:235:24 at Function.proto.process_params (D:\Workspace\contacts\node_modules\express\lib\router\index.js:313:12) at D:\Workspace\contacts\node_modules\express\lib\router\index.js:229:12 at Function.match_layer (D:\Workspace\contacts\node_modules\express\lib\router\index.js:296:3)
Does anyone have any suggestions? Any help would be greatly appreciated.
Share Improve this question edited Nov 17, 2014 at 12:21 George G 7,70512 gold badges48 silver badges61 bronze badges asked Nov 17, 2014 at 12:15 Lawrence PhillipsLawrence Phillips 311 silver badge2 bronze badges4 Answers
Reset to default 2var path = require('path');
res.sendFile(path.join(__dirname, './public', 'main.html'));
Try this:
res.sendfile(__dirname + '/public/main.html');
You have to specify an absolute path (starting with /)
You should change the path in your get('*') function to an absolute path:
res.sendfile('public/main.html');
You can use express' __dirname
for that.
Make sure that you access the public directory relative to the current working directory. Below changes should work in your case
var express = require('express'),
app = express(),
path = require('path'),
publicDir = path.join(__dirname, 'public');
app.use(express.static(publicDir))
app.get('*', function(req, res){
res.sendFile(path.join(publicDir, 'main.html'));
}).listen(3000);
本文标签: javascriptAngularjsTypeError path must be absolute or specify root to ressendFileStack Overflow
版权声明:本文标题:javascript - Angularjs - TypeError: path must be absolute or specify root to res.sendFile - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745262129a2650404.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论