如何部署MERN应用,我得到了“无法获得/”

时间:2019-01-08 04:02:30

标签: node.js reactjs azure express mern

我正在尝试在Azure上部署MERN应用程序,但是当我检查自己的URL时,我找不到错误cannot get /

我尝试在生产环境中的本地计算机上启动我的节点服务器,而我只有一个空白页面。

我尝试检查代码中是否没有路径问题,因此在server.js中,我的本地计算机上有console.log(path.resolve(__dirname, '../client/build'));,它向我显示了正确的路径,但是对{{1} },所以我猜天蓝色无法使用undefined

我的应用程序树如下:

__dirname

我的|--client |--build |--node_modules |--server |--server.js |--package.json

server.js

说实话,我不知道我希望得到什么。

当我在本地开发时,我的Node-js服务器也已启动,而我的react服务器也已启动。

当我尝试搜索有关如何部署的内容时,我只能找到说明“复制粘贴此内容,它将起作用”的教程,但我不知道它如何起作用?

在所有搜索中,我想我必须运行节点服务器,并且它将从构建存储库中的const dotenv = require('dotenv'); dotenv.config(); const express = require("express"); const mongoose = require("mongoose"); const bodyParser = require("body-parser"); const app = express(); const passport = require("passport"); const path = require('path'); const users = require("./routes/api/users"); // Bodyparser middleware app.use( bodyParser.urlencoded({ extended: false }) ); app.use(bodyParser.json()); // DB Config const db = require("./config/key.js").mongoURI; console.log(db); // Connect to MongoDB mongoose.Promise = global.Promise; mongoose.connect(db, { useNewUrlParser: true }) .then(() => console.log("MongoDB successfully connected")) .catch(err => console.log(err)); // Passport middleware app.use(passport.initialize()); // Passport config require("./config/passport")(passport); // Routes app.use("/api/users", users); // Serve static assets if in production if (process.env.NODE_ENV === 'production') { // Set static fold console.log(path.resolve(__dirname, '../client/build')); app.use(__dirname, express.static('../client/build')); app.get('*', (req, res) => { res.sendFile(path.resolve(__dirname, '../client', 'build', 'index.html')); }); } const port = process.env.PORT || 5000; app.listen(port, () => console.log('Server up and running on port ${port} !')); 渲染前端,然后为什么在天蓝的情况下得到index.html? >

## EDIT ##

实际上,问题在于Azure无法在生产模式下启动我的cannot get /,所以我的状况从未受到打击。

我已经没有这个server.js了,我在本地也遇到了同样的问题,我的页面空白。如果打开检查器,我可以看到它是我的index.html,并且可以在控制台中看到此错误     未捕获到的SyntaxError:意外的令牌

是空白页的原因吗?

0 个答案:

没有答案