当我在Heroku
上部署它时,我的应用程序运行正常。当我将其推送到AWS Elastic beantalk时,它会显示一个页面,内容为Cannot Get /
。
日志看起来像这样:
-------------------------------------
/var/log/nodejs/nodejs.log
-------------------------------------
> [email protected] dev /var/app/current
> run-p server start
sh: run-p: command not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! [email protected] dev: `run-p server start`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the [email protected] dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
Express server is running on localhost:8081
-------------------------------------
/var/log/nginx/error.log
-------------------------------------
2020/04/07 03:46:43 [error] 5546#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 206.188.72.122, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "reacttwilio-env.eba-ugm2xpnb.us-east-2.elasticbeanstalk.com"
2020/04/07 03:46:44 [error] 5546#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 206.188.72.122, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8081/favicon.ico", host: "reacttwilio-env.eba-ugm2xpnb.us-east-2.elasticbeanstalk.com", referrer: "http://reacttwilio-env.eba-ugm2xpnb.us-east-2.elasticbeanstalk.com/"
2020/04/07 04:04:00 [error] 5546#0: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 206.188.72.122, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "reacttwilio-env.eba-ugm2xpnb.us-east-2.elasticbeanstalk.com"
2020/04/07 04:04:00 [error] 5546#0: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 206.188.72.122, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8081/favicon.ico", host: "reacttwilio-env.eba-ugm2xpnb.us-east-2.elasticbeanstalk.com", referrer: "http://reacttwilio-env.eba-ugm2xpnb.us-east-2.elasticbeanstalk.com/"
这些每次打印大约10次。
package.json
{
"name": "react-express-starter",
"version": "0.1.0",
"private": true,
"dependencies": {
...
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"server": "node-env-run server --exec nodemon | pino-colada",
"server:prod": "node server",
"dev": "server start"
},
"proxy": "http://localhost:3001",
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"engines": {
"node": "12.x"
},
"devDependencies": {
...
}
}
服务器-> index.js
app.use(express.static(path.join(__dirname, "..", "build")));
const sendTokenResponse = (token, res) => {
res.set("Content-Type", "application/json");
res.send(
JSON.stringify({
token: token.toJwt()
})
);
};
app.get("/api/greeting", (req, res) => {
const name = req.query.name || "World";
res.setHeader("Content-Type", "application/json");
res.send(JSON.stringify({ greeting: `Hello ${name}!` }));
});
app.get("/video/token", (req, res) => {
const identity = req.query.identity;
const room = req.query.room;
const token = videoToken(identity, room, config);
sendTokenResponse(token, res);
});
app.post("/video/token", (req, res) => {
const identity = req.body.identity;
const room = req.body.room;
const token = videoToken(identity, room, config);
sendTokenResponse(token, res);
});
app.listen(config.port, () =>
console.log(`Express server is running on localhost:${config.port}`)
);
我尝试将脚本更改为npm run dev
,这是我在本地启动脚本的方式,但这给了我502 bad gateway
错误。
我看到的是,您没有为路由“ /”定义响应。您可以这样做: