I have a typeorm project with that structure:
In both client and root, index files are located in src directory. In package.json in client directory I changed one thing("build") and added proxy:
"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:
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:
...
"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".
Ok, it's good. I just passed invalid path to espress.static:
app.use(express.static('public'));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.