部署到Heroku的MERN应用无法正常运行

编程入门 行业动态 更新时间:2024-10-28 05:17:43
本文介绍了部署到Heroku的MERN应用无法正常运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经将MERN应用程序部署到了Heroku.当我转到该应用程序时,我可以通过我的API将数据发布到MongoDB数据库,但是,每当我发出GET请求时,Heroku都会做出以下响应:

I've deployed a MERN app to Heroku. When I go to the app, I'm able to post data through my APIs to the MongoDB database, however, whenever I make a GET request, Heroku responds with:

at=info method=GET path="/api/lists/5b44001a558fe30014e8c43c" host=bootcamp-bucket-list.herokuapp request_id=e9b06431-aa30-4811-bf7d-a46720991646 fwd="24.124.88.220" dyno=web.1 connect=0ms service=2ms status=304 bytes=237 protocol=https

我能够在服务器上本地运行该应用程序而不会出现任何问题,只是在我们进行生产时,GET请求失败.有没有人以前经历过这种情况,并且知道是什么原因导致了此问题?让我知道是否需要其他信息.

I am able to run the app locally on my servers without any issues, it's just when we're in prodution, the GET requests fail. Has anyone experienced this before and know what could be causing this issue? Let me know if any additional info is needed.

这是我的server.js文件的设置:

const express = require('express'); const path = require('path'); const users = require('./routes/api/users'); const routes = require('./routes'); const app = express(); const port = process.env.PORT || 5001; const bodyParser = require("body-parser"); const passport = require('passport'); const mongoose = require("mongoose"); const Models = require('./models'); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); mongoose.Promise = Promise; var MONGODB_URI = process.env.MONGODB_URI || "mongodb://localhost/testdb"; console.log(MONGODB_URI); mongoose.connect(MONGODB_URI); const db = mongoose.connection; app.use(passport.initialize()); // PASSPORT CONFIG require('./config/passport')(passport); app.get('*', function(req, res) { res.sendFile(path.join(__dirname, './client/build/index.html')); }); if (process.env.NODE_ENV === 'production') { // Serve any static files app.use(express.static('client/build')); } // USE ROUTES app.use('/api/users', users); app.use(routes); app.listen(port, () => console.log(`Listening on port ${port}`));

我的package.json文件中还包含以下脚本:

I also have the following scripts in my package.json file:

"start": "node server.js", "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"

推荐答案

您需要确保在生产模式下运行时,要保留api端点.

You need to make sure that when running in production mode that you are reserving your api endpoint.

app.get('*', function(req, res) { res.sendFile(path.join(__dirname, './client/build/index.html')); });

应该类似于

if (process.env.NODE_ENV === 'production') { app.use(express.static('client/build')); // serve the static react app app.get(/^\/(?!api).*/, (req, res) => { // don't serve api routes to react app res.sendFile(path.join(__dirname, './client/build/index.html')); }); console.log('Serving React App...'); };

更多推荐

部署到Heroku的MERN应用无法正常运行

本文发布于:2023-10-28 02:13:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535207.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正常运行   Heroku   MERN

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!