0

I have an react.js frontend application that is created with creact-react-app utility. I intend to run the app on a windows server machine and it has the following build/run tasks that are defined in my package.json file:

"scripts": {     
    "build:dev": "env-cmd -f .env.dev npm run build",
    "build:test": "env-cmd -f .env.test npm run build",
    "build:prep": "env-cmd -f .env.prep npm run build",
    "build:prod": "env-cmd -f .env.prod npm run build",
    "start-server": "env-cmd -f .env.prod node server/server.js"
  }

So, for preparing my app for deployment on a windows server, I issue the following while I am inside the OS with an open command console:

npm run build:prod
npm run start-server 

As a result of "npm run build:prod", the bundle is placed under my build directory in my project root. On the other hand, executing "npm run start-server" executes the following script, which is server/server.js.

const path = require('path');
const express = require('express');
const app = express();
const publicPath = path.join(__dirname, '..', 'build');
const port = process.env.REACT_APP_PORT;
app.use(express.static(publicPath));
app.get('*', (req, res) => {
    res.sendFile(path.join(publicPath, 'index.html'));
});
app.listen(port, () => {
    console.log('Server has started listening at port ' + port);
});

Everything is fine until here, my application works but it keeps the command console busy. What I need is to run the app in the background and even if I log out windows, the app should continue to run and respond to requests. Moreover, it should start after windows restart. What would be the most convenient approach to realize this? I have read the create-react-app documentation but I did not get much help. In addition I noticed there is an npm package named pm2, but my corporate regulations dont let me install it unfortunately.

QUESTION: So, briefly I need your suggesstions on what would be the best practice approach to run my react.js front-end app at the background such that it keeps alive after log off and get restarted after windows restart? Thanks.

F. Aydemir
  • 2,665
  • 5
  • 40
  • 60
  • There are quite a few answers on the internet about running a script on Win startup. Regarding logging out... I don't know, that's why servers are most often Unix machines :) – k-wasilewski Jan 19 '21 at 20:10
  • A Windows service, running the command as a user that stays logged in, using something like PM2... there are lots of options. The best option, like @k-wasilewski said, is to not host things on Windows unless they actually need to be on Windows (eg .NET Framework). – Zac Anger Jan 19 '21 at 20:25
  • See these other answers: [1](https://stackoverflow.com/questions/42854148/keep-node-in-running-state-even-after-user-log-off), [2](https://stackoverflow.com/questions/62991121/how-to-properly-run-nodejs-on-windows-server-in-production), [3](https://stackoverflow.com/questions/12701259/how-to-make-a-node-js-application-run-permanently). – Zac Anger Jan 19 '21 at 20:26

0 Answers0