我有一个具有该结构的typeorm项目:
在客户端和根目录中,索引文件都位于src目录中。 在客户端目录的package.json中,我更改了一件事(“ build”)并添加了代理:
"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"
在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}`));
以及根目录中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\""
}
当我在localhost上运行它时,它起作用了,但是当我在heroku上部署它时,它表明我在后端(在端口4000上)得到的就是“你好”。
答案 0 :(得分:0)
好的,很好。我只是将无效的路径传递给espress.static:
app.use(express.static('public'));