1

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.

Jonh
  • 95
  • 1
  • 2
  • 13

1 Answers1

3

Setup pm2 config

  1. cd to project folder

  2. Create ecosystem.config.js file for pm2 with the following config

module.exports = { apps : [{ name : 'APPNAME', script : './index.js', env: { NODE_ENV: 'development' }, env_production : { NODE_ENV: 'production' } }], };

  1. Start app's process using pm2

For production : pm2 start --env production

For development : pm2 start --env development

Some basic pm2 commands

  • Stop App : pm2 stop APPNAME
  • Start App : pm2 start APPNAME
  • Monitor App : pm2 monit APPNAME
  • Delete App : pm2 delete APPNAME
  • Show list of running pm2 processes : pm2 list

Hope this helps!

  • If you have already have a `config.js` file then just add those lines in config.js file. – Abhishek Panchal Feb 12 '20 at 08:47
  • Excuse me, Abhishek Panchal, it still not working, I don't know why it's still not working. – Jonh Feb 12 '20 at 08:50
  • I create ecosystem.config.js file in the project directory, but it still can not access my application. – Jonh Feb 12 '20 at 08:52
  • Is it a problem with the server.js file, can you help to check with that server.js, review my code? Thank Abhishek Panchal. – Jonh Feb 12 '20 at 08:56
  • I am checking, will let you know! – Abhishek Panchal Feb 12 '20 at 08:56
  • Yes, please. Thank you, Abhishek Panchal. – Jonh Feb 12 '20 at 09:00
  • Can you please write pm2 logs and check you have any error log there? Because i am checking and it's working fine! – Abhishek Panchal Feb 12 '20 at 09:53
  • Please remove ecosystem.config.js file and create using `pm2 ecosystem`, it will create one new file where you just need to update script: `'./server.js',`, aftr update just run command `pm2 start ecosystem.config.js` and will start process. – Abhishek Panchal Feb 12 '20 at 09:56
  • After `pm2 start ecosystem.config.js` please run command `pm2 logs` and check you are getting any logs in it. – Abhishek Panchal Feb 12 '20 at 09:58
  • Can you check the log file below post. – Jonh Feb 12 '20 at 10:26
  • @Jonh It's because your port is already in use, you must need to kill that port before to start. run command `netstat -lpn |grep :8080`, then you will get log with that port number then you need to kill port with it's process id. `kill -9 4541` – Abhishek Panchal Feb 12 '20 at 10:32
  • I found this solution, try it Give permission to use ```sudo``` ```sudo pkill node```, Now I able to run the server, thank you very much for follow up on my issue. You are awesome. Again, a big thanks to you, man. – Jonh Feb 12 '20 at 10:49
  • Great, Welcome! – Abhishek Panchal Feb 12 '20 at 10:55
  • Hi, Abhishek Panchal, I wonder about my application that is still facing some issues, what I want to ask is, actually in MERN stack application there is 2 ports, 1 port for the front end ```3000```, and 1 port for the node.js server ```8080```, but when we use Pm2 in the deployment I only can access my application through port ```8080``` only, but if I use ```npm run dev``` without using pm2, I can access through port ```3000```. I wonder what is different between them. Is it the same or what? – Jonh Feb 13 '20 at 06:54
  • Because I'm facing this problem now, ``` Refused to load the image 'http://104.248.153.121:8080/favicon.ico' because it violates the following Content Security Policy directive: "default-src 'none'". Note that 'img-src' was not explicitly set, so 'default-src' is used as a fallback. ``` I feel like I disturb you a lot, but if you have time, please help me. Thank Abhishek Panchal. I could not access my application when I accessed it shows ```Cannot GET /``` . – Jonh Feb 13 '20 at 08:04
  • Note: But if I run ```npm run dev``` and access through port ```3000```, everything is working fine, I don't know what happened with pm2 or what? – Jonh Feb 13 '20 at 08:15
  • You are delete/stop pm2 existing process before to start again pm2 process? you must need to stop existing same process. – Abhishek Panchal Feb 13 '20 at 09:15
  • Yes, I stop pm2/delete/restart service but still I got this error log ``` Refused to load the image 104.248.153.121:8080/favicon.ico because it violates the following Content Security Policy directive: default-src none. Note that img-src was not explicitly set, so default-src is used as a fallback ``` and I could not use my application, if I reload the page it show ```Cannot Get /```, it happend in every page of my application if I tried to reload the page. I don't know what happened? Please help. – Jonh Feb 13 '20 at 09:46
  • Detail Issue: https://stackoverflow.com/questions/60200673/react-js-refuse-to-load-the-image-because-it-violates-the-following-content-secu – Jonh Feb 13 '20 at 09:54
  • I think if we can make pm2 start app on port 3000, it's going to be fine because port 3000 is working fine at the front end and port 8080 is a port for node js server that's why it said violate the url, just my idea. Can we do that? – Jonh Feb 13 '20 at 10:22
  • No, that issue is not because of port, it's because Content Security Policy. i have added answer link if it helps! – Abhishek Panchal Feb 13 '20 at 12:27
  • 1
    Thank you, Abhishek Panchal. – Jonh Feb 14 '20 at 10:11