Configuring a proxy server to create an application application

I started the responsive application using create-react-app and ran the npm run eject script to access all the files. Then I installed the expressed and created server.js file, which is at the same level as the package.json file

this is server.js file contents:

 const express = require('express'); const app = express; app.set('port', 3031); if(process.env.NODE_ENV === 'production') { app.use(express.static('build')); } app.listen(app.get('port'), () => { console.log(`Server started at: http://localhost:${app.get('port')}/`); }) 

Nothing crazy here, just a setup for future api proxies where I need to use secrets, and since I don't want to reveal my api.

after that I added the file "proxy": "http://localhost:3001/" to my package.json . I'm stuck now, because I need to figure out how to properly start my server and use this server.js file in development mode and then in production.

Ideally, it would be nice if we could use more than one proxy, i.e. /api and /api2

+6
source share
1 answer

You did not need to throw server.js to start. You can simply run it using node server.js along with create-react-app .

You can still do npm start even after extraction to start your dev server.

To run /api1 and /api2 you just need to process it in the server.js file and it should work fine. You must match the port in server.js and in the proxy settings inside package.json - in this case it should be "proxy": "http://localhost:3031"

+5
source

Source: https://habr.com/ru/post/1012111/


All Articles