pouria daneshvar
pouria daneshvar

Reputation: 35

node.js - TypeError: path must be absolute or specify root to res.sendFile

I'm trying to figure out what is the source of this problem. According to this page, everything should work fine, but i get this error.

TypeError: path must be absolute or specify root to res.sendFile
at ServerResponse.sendFile (/Users/apple/Documents/JavsScript/test/node_modules/express/lib/response.js:410:11)
at /Users/apple/Documents/JavsScript/test/server.js:13:9
at Layer.handle [as handle_request] (/Users/apple/Documents/JavsScript/test/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/apple/Documents/JavsScript/test/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/apple/Documents/JavsScript/test/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/apple/Documents/JavsScript/test/node_modules/express/lib/router/layer.js:95:5)
at /Users/apple/Documents/JavsScript/test/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/apple/Documents/JavsScript/test/node_modules/express/lib/router/index.js:335:12)
at next (/Users/apple/Documents/JavsScript/test/node_modules/express/lib/router/index.js:275:10)
at SendStream.error (/Users/apple/Documents/JavsScript/test/node_modules/serve-static/index.js:121:7)

Here's my code:

var host = '127.0.0.1';
var port = 8020;

var express = require('express');
var path = require('path');

var app = express();

app.use(express.static(path.join(__dirname, 'Public')))

app.get('/', function (req, res) {
    res.sendFile('index.html');
});

app.listen(port, host, function () {
    console.log('App listening on ' + host + ':' + port);
});

and here's my directory structure:

server.js
node_modules
package.json
Public  
    views
       inde.html

Upvotes: 0

Views: 2576

Answers (2)

Newbie
Newbie

Reputation: 21

res.sendFile('Public/views/inde.html' , { root : __dirname}); worked for me, just wanted to give some credit!

Upvotes: 0

user7312892
user7312892

Reputation:

Just try this:

res.sendFile('Public/views/inde.html' , { root : __dirname});

Upvotes: 1

Related Questions