简体   繁体   English

为什么应用程序在 localhost 上运行但在部署时导致 CANNOT GET /?

[英]Why does application work on localhost but results in CANNOT GET / when deployed?

So I built myself a personal portfolio website using the MERN stack, and everything was working well on localhost, so I decided to deploy it using Google App Engine.因此,我使用 MERN 堆栈为自己构建了一个个人作品集网站,并且一切都在 localhost 上运行良好,因此我决定使用 Google App Engine 部署它。 I delpoyed the frontend and backend as two seperate services as recommended by this tutorial.根据教程的建议,我将前端和后端作为两个单独的服务进行了扩展。 The frontend works great (see https://personal-website-279319.wl.r.appspot.com/ ), but the backend doesn't.前端效果很好(参见https://personal-website-279319.wl.r.appspot.com/ ),但后端没有。 If you look under the "project" tab, nothing shows up as the backend is unable to connect to Mongo Atlas and actually retrieve the data.如果您在“项目”选项卡下查看,则不会显示任何内容,因为后端无法连接到 Mongo Atlas 并实际检索数据。

The code for server.js, the start page for the backend is as follows: server.js 的代码,后端的起始页如下:

require('@google-cloud/debug-agent').start();
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
require('dotenv').config();

// Initialize Dependnecies and Set Port?
const app = express();
const port = process.env.PORT || 5000;

app.use(cors());
app.use(express.json());

// Connect To Mongoose
const uri = process.env.ATLAS_URI;
mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true });

const connection = mongoose.connection;
connection.once('open', () => {
  console.log("MongoDB database connection established successfully");
})

// Connect URL Ending to Router
const contactRouter = require('./routes/contact');
const projectRouter = require('./routes/project');

app.use('/contact', contactRouter);
app.use('/projects', projectRouter);

// Listen for Connection on Port 
app.listen(port, () => {
    console.log(`Server is running on port: ${port}`);
});

The rest of the code can be found on the Github repository here if you'd like to see.如果您想查看,可以在此处的 Github 存储库中找到代码的 rest。 I did make sure this script would run on start by putting this snippet of code in package.json.我确实通过将这段代码放入 package.json 来确保该脚本将在开始时运行。

  "scripts": {
    "start": "node server.js",
    "test": "echo \"Error: no test specified\" && exit 1"

When I go to the service api ( https://api-dot-personal-website-279319.wl.r.appspot.com/ ) directly, I get a page saying Cannot Get /. When I go to the service api ( https://api-dot-personal-website-279319.wl.r.appspot.com/ ) directly, I get a page saying Cannot Get /. I checked the console and the message that's supposed to say the "connection to MongoDB established successfully" isn't there.我检查了控制台,应该说“与 MongoDB 的连接已成功建立”的消息不存在。 I did get a message saying "Content Security Policy" was blocking the loading of some favicon (I'm not sure why.) I tried to fix this by changing the /routes/projects code to try to prevent this我确实收到一条消息,说“内容安全策略”阻止加载某些网站图标(我不知道为什么。)我试图通过更改 /routes/projects 代码来解决这个问题,以防止这种情况发生

/* Route Get (Everything) Request */
router.route('/').get((req, res) => {
    res.setHeader("Content-Security-Policy", "");
    Project.find()
    .then(projects => res.json(projects))
    .catch(err => res.status(400).json('Error: ' + err));
});

but that resulted in a 500 Service Error once I deployed it.但是一旦我部署它就会导致 500 服务错误。 Honestly, I'm really confused;老实说,我真的很困惑。 I think I'm missing something, but I'm not sure what.我想我错过了一些东西,但我不确定是什么。 If someone could explain what I'm doing wrong or lead me in the right direction, that would be much appreciated, thanks.如果有人可以解释我做错了什么或引导我朝着正确的方向前进,那将不胜感激,谢谢。

You're Atlas URI is determined from process.env.ATLAS_URI .您的 Atlas URI 由process.env.ATLAS_URI确定。 If you're using App Engine (standard) you'll want to ensure that the app.yaml file includes a value for this ATLAS_URI environment variable.如果您使用的是 App Engine(标准),则需要确保app.yaml文件包含此ATLAS_URI环境变量的值。 I suspect you'll need more than just the database URI and may need username|password environment variables too.我怀疑您需要的不仅仅是数据库 URI,还可能需要用户名|密码环境变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM