无法准确解读错误:TypeError:路径必须是绝对路径或在 ServerResponse.sendFile 处指定 res.sendFile 的根

Posted

技术标签:

【中文标题】无法准确解读错误:TypeError:路径必须是绝对路径或在 ServerResponse.sendFile 处指定 res.sendFile 的根【英文标题】:Can't precisely decipher error: TypeError: path must be absolute or specify root to res.sendFile at ServerResponse.sendFile 【发布时间】:2018-03-18 19:49:10 【问题描述】:

我是 react/redux/mongoose/passport 菜鸟。我认为它应该通过 axios 将响应发送回反应页面,但似乎并非如此。

我遇到了这个非常奇怪的错误,类似于在this link 发现的问题。

GET /robots.txt 500 6.666 ms - 2143
TypeError: path must be absolute or specify root to res.sendFile
at ServerResponse.sendFile (/Users/eric/Documents/todobuild/node_modules/express/lib/response.js:421:11)
at /Users/eric/Documents/todobuild/server.js:40:12
at Layer.handle [as handle_request] (/Users/eric/Documents/todobuild/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/eric/Documents/todobuild/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/eric/Documents/todobuild/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/eric/Documents/todobuild/node_modules/express/lib/router/layer.js:95:5)
at /Users/eric/Documents/todobuild/node_modules/express/lib/router/index.js:281:22
at param (/Users/eric/Documents/todobuild/node_modules/express/lib/router/index.js:354:14)
at param (/Users/eric/Documents/todobuild/node_modules/express/lib/router/index.js:365:14)
at Function.process_params (/Users/eric/Documents/todobuild/node_modules/express/lib/router/index.js:410:3)
at next (/Users/eric/Documents/todobuild/node_modules/express/lib/router/index.js:275:10)
at SessionStrategy.strategy.pass (/Users/eric/Documents/todobuild/node_modules/passport/lib/middleware/authenticate.js:338:9)
at SessionStrategy.authenticate (/Users/eric/Documents/todobuild/node_modules/passport/lib/strategies/session.js:75:10)
at attempt (/Users/eric/Documents/todobuild/node_modules/passport/lib/middleware/authenticate.js:361:16)
at authenticate (/Users/eric/Documents/todobuild/node_modules/passport/lib/middleware/authenticate.js:362:7)
at Layer.handle [as handle_request] (/Users/eric/Documents/todobuild/node_modules/express/lib/router/layer.js:95:5)

据我所知,我们的情况之间的区别在于他们在启动节点服务器时遇到了错误,但我的启动正常。只有在我设置了我的第一个 API 请求之后,我才开始遇到这个问题。

问题脚本如下:

const mongoose = require('mongoose');
const passport = require('passport');
const User = require('../../models/user.js');

module.exports = function(app) 

app.post('/api/login', function(request, response) 
  User.findOne( email: request.body.email )
    .then(function(data) 
      if (data.email) 
        passport.authenticate('local')(request, response, () => 
          if (request.user) 
            return response.send(JSON.stringify(request.user));
          
        );
      

      return response.send(JSON.stringify( error: 'There was an error logging in.'));
    )
    .catch(error => console.log(error));
);

这也是我的 server.js 文件。

const path = require('path');
const express = require('express');
const logger = require('morgan');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const expressSession = require('express-session')(
  secret: 'some random string',
  resave: false,
  saveUninitialized: false
);

const app = express();
const PORT = process.env.PORT || 3000;

mongoose.connect('mongodb://localhost/todo', useMongoClient: true 
);
const db = mongoose.connection;
//models
const User = require('./models/user');

app.use(express.static(path.join(__dirname, './public')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded( extended: false ));
app.use(bodyParser.text());
app.use(bodyParser.json( type: 'application/vnd.api+json' ));
app.use(expressSession);
app.use(passport.initialize());
app.use(passport.session());

passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

//routes
require('./routes/api/api-routes.js')(app);

app.get('*', function(request, response) 
  response.sendFile('index.html');
);

db.on('error', function(error) 
  console.log('Mongoose Error: ', error);
);

db.once('open', function() 
  console.log('Mongoose connection successful.');
);

app.listen(PORT, 'localhost', function() 
  console.log('App running on port 3000');
);

依赖关系

"dependencies": 
    "axios": "^0.16.2",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-import-export": "^1.0.2",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-2": "^6.24.1",
    "body-parser": "^1.18.2",
    "express": "^4.16.1",
    "express-session": "^1.15.6",
    "history": "^4.7.2",
    "mongoose": "^4.12.0",
    "morgan": "^1.9.0",
    "passport": "^0.4.0",
    "passport-local": "^1.0.0",
    "passport-local-mongoose": "^4.2.1",
    "react": "^16.0.0",
    "react-dom": "^16.0.0",
    "react-redux": "^5.0.6",
    "react-router": "^4.2.0",
    "react-router-dom": "^4.2.2",
    "react-router-redux": "^4.0.8",
    "redux": "^3.7.2",
    "webpack": "^3.6.0"
  

再次,我为我的无知道歉。 :(

【问题讨论】:

【参考方案1】:

堆栈跟踪是这样说的:

在 /Users/eric/Documents/todobuild/server.js:40:12

所以问题是server.js 中的第 40 行。有根据的猜测表明第 40 行就是这一行:

response.sendFile('index.html');

sendFile 的文档在这里:

https://expressjs.com/en/4x/api.html#res.sendFile

关键是您使用的是相对文件路径index.htmlsendFile 需要绝对路径,例如:

response.sendFile(path.join(__dirname, 'index.html'));

【讨论】:

以上是关于无法准确解读错误:TypeError:路径必须是绝对路径或在 ServerResponse.sendFile 处指定 res.sendFile 的根的主要内容,如果未能解决你的问题,请参考以下文章

图片上传错误:TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。接收类型未定义

Nrwl:Storybook 给我错误 TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。收到未定义

TypeError:尝试上传图像时无法读取未定义的属性“路径”

react-router-dom - TypeError:无法读取未定义的属性(读取“路径名”)

TypeError: function() 参数 1 必须是代码,而不是 str

升级依赖项时反应“TypeError:路径必须是字符串。收到未定义”