javascript - Angularjs - TypeError: path must be absolute or specify root to res.sendFile - Stack Overflow

I am trying to create a contacts app with angularjs. I have created a file in the root directory of the

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 badges
Add a ment  | 

4 Answers 4

Reset to default 2
var 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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745262130a4619259.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信