TypeError:path必须是绝对路径或指定root到res.sendFile

时间:2016-05-31 10:31:15

标签: javascript node.js mongodb

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) 

我尝试通过连接到http://localhost:3000来显示todo.html页面,但是当我打开http://localhost:3000页面时出现错误,我上面已经将错误上传为一个片段。你能指导我如何显示html文件吗?

1 个答案:

答案 0 :(得分:2)

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

<强>更新

尝试使用

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

@robin

查看快速源代码,表达/ 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
};

它只检查\\,我认为这是一个错误。