I able to set up the pm2 successfully with my MERN Stack application, but when I tried to run pm2 start server.js
and it shows status online
but I was not able to access my MERN stack application in the browser, but when I run my application without using pm2 npm run dev
with Nodemon everything was working fine. Below is my server.js file
const express = require('express');
const mongoose = require('mongoose');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const cors = require('cors');
require('dotenv').config();
const config = require('config');
const path = require('path');
const devPort = 8080;
// Setup express app
const app = express();
app.use(cors());
app.options('*', cors());
app.use(express.json());
app.use(morgan('combined'));
app.use(bodyParser.urlencoded({ extended: true }));
mongoose.Promise = global.Promise;
const db = config.get('MONGODB_URI');
mongoose.connect(db, {
useUnifiedTopology: true,
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false
});
mongoose.connection
.once('open', () => console.log('Database connected!'))
.on('error', error => console.log('Could not connect', error));
/*route/api/file is here*/
app.use('/api/user', require('./route/api/user'));
//server static assets in production
if (process.env.NODE_ENV === 'production') {
//set static folder
app.user(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.htm'));
});
}
app.listen(process.env.PORT || devPort, () =>
console.log(`Node JS is running on port ${devPort}`)
);
here is an image of pm2 server.js online in the terminal but I could not access it. I wondered, is it the problem with the server.js file, please give suggestions. Thank you for your help.