类型错误:路径必须是绝对路径或指定 root 到 res.sendFile [英] TypeError: path must be absolute or specify root to res.sendFile

查看:56
本文介绍了类型错误:路径必须是绝对路径或指定 root 到 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) 

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

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 ?

推荐答案

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

更新

尝试使用

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

@罗宾

查看express源码,express/lib/utils.js

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.

这篇关于类型错误:路径必须是绝对路径或指定 root 到 res.sendFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆