简体   繁体   English

将前端部署到 heroku 时出现问题

[英]Problem with deploying frontend to heroku

I have a typeorm project with that structure:我有一个具有这种结构的 typeorm 项目:

在此处输入图像描述

In both client and root, index files are located in src directory.在客户端和根目录中,索引文件都位于 src 目录中。 In package.json in client directory I changed one thing("build") and added proxy:在客户端目录中的 package.json 中,我更改了一件事(“构建”)并添加了代理:

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build && mv build ../public",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "gen": "graphql-codegen --config codegen.yml"
  },
"proxy": "http://localhost:4000"

In src/index.ts I made that:在 src/index.ts 我做到了:

const app = express();
...
app.get("/", (_req, res) => res.send("hello"));
app.use(express.static('../public'));

    app.get('*', (_, res) => {
      res.sendFile(path.resolve(__dirname, '../public', 'index.html'))
    });

  
    const port = process.env.PORT || 4000;
  
    app.listen(port, () => console.log(`Server started on port ${port}`));

And part of package.json from root directory:以及来自根目录的 package.json 的一部分:

...
"scripts": {
      "start": "ts-node src/index.ts",
      "server": "nodemon --exec ts-node src/index.ts",
      "client": "npm start --prefix client",
      "dev": "concurrently \"npm run server\" \"npm run client\""
   }

And it works when I run it on localhost, but when I deployed on heroku it shows me that, what I get on backend(on port 4000), that is this "hello".当我在本地主机上运行它时它可以工作,但是当我部署在 heroku 上时,它向我展示了我在后端(在端口 4000 上)得到的,就是这个“你好”。

Ok, it's good.好的,很好。 I just passed invalid path to espress.static:我刚刚将无效路径传递给 espress.static:

app.use(express.static('public'));

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

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