无法正确解密错误:TypeError:path必须是绝对的,或者在ServerResponse.sendFile

时间:2017-10-06 23:50:10

标签: node.js reactjs express redux passport.js

我是react / redux / mongoose / passport noob。我认为它应该通过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 个答案:

答案 0 :(得分:1)

堆栈跟踪说明了这一点:

  

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

所以问题是foo[i].Xcdnt中的第40行。一个有根据的猜测表明第40行是这一行:

server.js

response.sendFile('index.html'); 的文档在这里:

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

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

sendFile