Robin Robin - 6 months ago 151
Node.js Question

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



var express = require('express');
var app = express();
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
app.use(bodyParser.json());

mongoose.connect("mongodb://localhost/test");

var todoschema = new mongoose.Schema ({
name : {type: String, required: true}
});

var todomodel = mongoose.model('todolist',todoschema);

app.get('/',function(req,res){

res.sendFile('C:/Users/Rohit/Desktop/New folder/public/todo.html');
})

app.get('/todolist', function (req, res){
todomodel.find(function(err,tasks){
res.json(tasks);
});
});

app.post('/todolist', function (req, res) {

todomodel.insert(req.body, function(err, task) {
res.json(task);
});
});

app.delete('/todolist/:id', function (req, res) {

todomodel.remove(req.params.id, function (err, task) {
res.json(task);
});
});

app.get('/todolist/:id', function (req, res) {

todomodel.findById(req.params.id, function (err, task) {
res.json(task);
});
});

app.put('/todolist/:id', function (req, res) {

todomodel.findAndModify({
query: req.params.id,
update: {$set: {name: req.body.name}},
new: true}, function (err, task) {

res.json(task);
}
);
});

app.listen(3000);
console.log("Server running on port 3000");







TypeError: path must be absolute or specify root to res.sendFile
at ServerResponse.sendFile (C:\Users\Rohit\node_modules\express\lib\response.js:403:11)
at C:\Users\Rohit\Desktop\New folder\mong.js:17:9
at Layer.handle [as handle_request] (C:\Users\Rohit\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Rohit\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch (C:\Users\Rohit\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Rohit\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Rohit\node_modules\express\lib\router\index.js:277:22
at Function.process_params (C:\Users\Rohit\node_modules\express\lib\router\index.js:330:12)
at next (C:\Users\Rohit\node_modules\express\lib\router\index.js:271:10)
at jsonParser (C:\Users\Rohit\node_modules\body-parser\lib\types\json.js:100:40)
at Layer.handle [as handle_request] (C:\Users\Rohit\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\Rohit\node_modules\express\lib\router\index.js:312:13)
at C:\Users\Rohit\node_modules\express\lib\router\index.js:280:7
at Function.process_params (C:\Users\Rohit\node_modules\express\lib\router\index.js:330:12)
at next (C:\Users\Rohit\node_modules\express\lib\router\index.js:271:10)
at expressInit (C:\Users\Rohit\node_modules\express\lib\middleware\init.js:33:5)





I tried to display a todo.html page by connecting to http://localhost:3000 , but I'm getting an error when i open the http://localhost:3000 page , the error i have uploaded above as a snippet. Can you please guide me on how to display the html file ?

Answer
res.sendFile("C:/Users/Rohit/Desktop/New folder/public/todo.html");

UPDATE

try to use

res.sendFile("C:\\Users\\Rohit\\Desktop\\New folder\\public\\todo.html");

@robin

look at the express source code, express/lib/utils.js

exports.isAbsolute = function(path){
  if ('/' == path[0]) return true;
  if (':' == path[1] && '\\' == path[2]) return true;
  if ('\\\\' == path.substring(0, 2)) return true; // Microsoft Azure absolute path
};

It only check the \\, i think it is a bug.

Comments