如何解决 AWS 部署中的“Cannot Get /”错误?

Posted

技术标签:

【中文标题】如何解决 AWS 部署中的“Cannot Get /”错误?【英文标题】:How to solve "Cannot Get /" error on AWS deployment? 【发布时间】:2020-07-19 22:17:21 【问题描述】:

当我在Heroku 上部署应用程序时,它运行良好。当我将它推送到 AWS 弹性豆茎时,它会给我一个页面,上面写着Cannot Get /

日志如下所示:

-------------------------------------
/var/log/nodejs/nodejs.log
-------------------------------------

> react-express-starter@0.1.0 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! react-express-starter@0.1.0 dev: `run-p server start`
npm ERR! spawn ENOENT
npm ERR! 
npm ERR! Failed at the react-express-starter@0.1.0 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 错误。

【问题讨论】:

你真正的错误是这个run-p: command not found。我不知道run-p是什么,但是看起来你需要安装它。 @Jordanm 我可以用什么代替它? 替换是什么意思?安装工具 它给了我一个页面,上面写着Cannot Get / 当我从 /video/token 请求某些东西时,它会给我令牌。这是预期的。但是只有主页没有显示。 【参考方案1】:

我看到的是,您没有为路由“/”定义响应。 你可以这样做:

var express = require('express');
var app = express();

// respond with "hello world" when a GET request is made to the homepage
app.get('/', function(req, res) 
  res.send('hello world');
);

问题在于,当您将其推送到弹性 beantalk 时,默认情况下它会转到根站点。也许您可以重定向到其他路线以避免这种行为。

【讨论】:

是的,这是有效的,我想作为响应发送的是一个用React 编写的组件。当我需要该组件并尝试将其发回时,它会告诉我 Cannot use import statement outside a module. 我可以看看你的完整代码吗?也许通过github? 一旦你得到它就知道了。我将在这里删除我的回购。 我得到了链接。我没有找到您在哪里请求组件并发送。我应该看到什么文件? 请查看服务器文件夹,然后查看 index.js

以上是关于如何解决 AWS 部署中的“Cannot Get /”错误?的主要内容,如果未能解决你的问题,请参考以下文章

如何终止 AWS Opsworks 中的部署?

针对windows系统如何解决openssl_pkey_export(): cannot get key from parameter 1这个问题

如何部署我的 AWS 解决方案以避免 CORS 问题?

如何使用aws elasticbeanstalk中的钩子运行部署后脚本?

如何访问部署在 aws elasticbeanstalk 中的文件

Cannot get a connection, pool exhausted解决办法