How to Host React App using Express

How to Host React App using Express

Create a <strong>package.json</strong> file containing following contents.

{
    "name": "app_name",
    "version": "1.0.0",
    "description": "",
    "main": "app.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "author": "",
    "license": "ISC",
    "dependencies": {
        "express": "^4.16.3",
        "path": "^0.12.7",
        "react-color": "^2.18.1"
    }
}
npm install

Now Create <strong>app.js</strong> file containing following contents.

const express = require('express');
const path = require('path');
const app = express();

app.use(express.static(path.join(**dirname, 'build')));
app.get('/\*', function (req, res) {
res.sendFile(path.join(**dirname, 'build', 'index.html'));
});
app.listen(3000);

Install pm2 for forever running node app.


npm i pm2 -g

Upload build (npm run build) folder along with app.js Start Server


pm2 start app.js

now app is running on specified port in app.js