Categories
centos express node.js service

Start node.js express server as a service

I’m currently preparing a react web app for production have set up a centos machine to host the app using node.js and express. I’d like to automate the starting of the express server.

My file structure is as follows:

/opt/
|__ /express/
|__ /node_modules/~
|__ express.js
|__ /public/
|__ bundle.js
|__ styles.css
|__ index.html

Once the react web app is developed locally, it’s packaged into a distribution and then moved into the public folder as seen above. express.js then starts the server and serves the page:

var path = require('path');
var express = require('express');
var app = express();
var port = 8000;
app.use(express.static('./public'));
app.get('*', (req, res) => {
res.sendfile(path.resolve(__dirname, 'public/index.html'));
});
app.listen(port, () => {
console.log('server running on port ' + port);
});

This works fine but requires me to manually start the server every time. I would like now to run express.js as an operating system level service so that its persistent and automatically starts up on boot/restart.

Ideally I would like this to be managed purely by the OS and not us any other npm packages.

How can this be done?