edit: Everything in this post is still accurate, but I've just noticed that the issue might be specifically when running pm2 start server --watch
instead of pm2 start server
. This makes even less sense to me. Updated title.
So I have a backend written in Node.js that works as a sort of REST API. And I have a frontend in a separate project written in Vue.js which is only run locally (not hosted on a server) and is used to make requests and interface with my remotely hosted Node.js/Express API.
I'm making 2 requests on one of my Vue.js webpages to my Node backend using Axios. In the backend, one of those requests routes to my online database, and one request routes to Twilio's API. The issue I'm having is that when I SSH into my server and start my server with a simple node server
command, the page loads fine and returns data for both requests. However, when I instead have PM2 run the server with a pm2 start server --watch
command, only ONE of the two requests on the web page will return data. The other request will throw the following error:
Access to XMLHttpRequest at 'https://*MYREMOTERESTAPI*' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Sometimes it is the Mongo request that returns the error while the Twilio returns data, and sometimes it is the Twilio request that returns the error while Mongo returns data. It is random but favors Mongo I believe because it responds first.
Some code from the server:
const express = require('express');
const app = express();
const cors = require('cors');
const fs = require('fs');
const morgan = require('morgan');
const winston = require('winston');
const path = require('path');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
app.use(cors());
mongoose.connect(MY SERVER INFO)
I have even added a .htaccess file to my server with the following code (to no effect):
Header always set Access-Control-Allow-Origin "http://localhost:8080"
Header always set Access-Control-Allow-Headers "Content-Type, Accept-Language, X-Access-Token, X-Client-Id, X-Secret-Id, X-GR-Token"
Header always set Access-Control-Allow-Methods "GET,POST,OPTIONS"
Header always set Access-Control-Expose-Headers "X-Access-Token, X-Refresh-Token,X-Access-Token-Expire, X-Pagination-Current-Page, X-Pagination-Page-Count,X-Pagination-Per-Page, X-Pagination-Total-Count, X-Payload"
Header always set Access-Control-Allow-Credentials "true"
After realizing the issue was related specifically to pm2's "watch" flag (see edit at top of question) I was able to narrow down the issue. Each time a request was made to my RESTful API, it updated the log file on the server. Each time the log file was updated, pm2 would notice a file change and restart the server, never getting to send a response to the second request.
I fixed this by creating an ecosystem.config.js file for pm2 in the same directory on my server as my server/app js files, and utilizing the "ignore_watch" key to instruct pm2 to ignore changes to my logs folder. Here are the contents of that file:
module.exports = {
apps : [{
name: 'my-pm2-process-name',
script: 'server.js',
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
instances: 1,
watch: true,
ignore_watch: ["logs"],
autorestart: true,
max_memory_restart: '1G',
env: {
NODE_ENV: 'development'
},
env_production: {
NODE_ENV: 'production'
}
}]
};