部署Heroku后后端节点路由不起作用

时间:2020-08-23 15:49:59

标签: node.js heroku

我最近使用Heroku部署了我的node.js后端,但现在看来我的路线不再可用。它总是通过我的错误路线返回“找不到此路线”。当我在开发过程中使用localhost时,不会发生这种情况。

app.js:

const express = require('express');
const bodyParser = require('body-parser');

const HttpError = require('./models/httpError');
const questionRoutes = require('./routes/questionRoutes');

const app = express();

app.use(bodyParser.json());

app.use((req, res, next) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader(
      'Access-Control-Allow-Headers',
      'Origin, X-Requested-With, Content-Type, Accept, Authorization'
    );
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PATCH, DELETE');
  
    next();
});

app.use('/api/questions', questionRoutes);

app.use((req, res, next) => {
    console.log(req.url);
    const error = new HttpError('Could not find this route.', 404);
    throw error;
});

app.use((error, req, res, next) => {
if (res.headerSent) {
    return next(error);
}
res.status(error.code || 500);
res.json({ message: error.message || 'An unknown error occurred!' });
});


app.listen(process.env.PORT || 5000);

我没有使用任何环境变量。这是我后端的heroku链接:https://mealoftheday.herokuapp.com/

任何帮助将不胜感激!

0 个答案:

没有答案