NodeJS/Express 503 error stemming from Forever

58 views Asked by At

edit: The problem was with forever-monitor. For some reason when I removed this and ran the server outright it worked right away.

I have a site which is an angular frontend and nodejs/express backend. It works perfectly on localhost, but when it is hosted (using a nodejs in cpanel) I only get 503 errors, which I believe stem from CORS issues.

  • I have tested in POSTMAN as get the 503 error
  • After reading SO I tried using "app.use(cors())" but it has the same issue.
  • They are hosted on the same server (frontend is www.server.com and backend is be.server.com, both https)
  • The port is 4040 though when contacting the A2 Hosting support about the exact same previous issue (which was resolved though I dont know how, because what I have currently is a copy and paste of that file... which works) they said not to use the port in the API call from the live version.
  • When I run the startup file in the terminal in cpanel there are no errors and it looks to be running as expected, however calling the server when launched that way results in a 404 error.

Information which may be of use is that when I just try to navigate to be.server.com then it also hangs with a 503 error. It doesnt say "cannot GET..." or whatever it should say.

This is my app.js which is the express portion and deals with cors:

const express = require('express');
const bodyParser = require('body-parser');
const app = express();

// added this bit after reading other threads on SO
app.options("*", (req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,POST,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Authorization, Content-Type');        
    res.sendStatus(200).end();
});

app.use((req, res, next) => {
    console.log('Request received:', req.method, req.url);
    console.log('Headers:', req.headers);
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,POST,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Authorization, Content-Type');
    next();
});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

const userRoutes = require('./routes/user');

// all the various api calls...
app.use("/api/user", userRoutes);

module.exports = app;

and example of how I am calling the API from my app is:

this.http.post<DatabaseReturn>(`https://be.server.com/api/user/login`, { email, password }).pipe(take(1));

I do set one additional header in the front end:

// set an Authorization header with the value "Bearer <their unique token OR undefined>"
headers: req.headers.set('Authorization', "Bearer " + token)

The error I get in the console is:

XHROPTIONS https://be.server.com/api/user/login CORS Missing Allow Origin

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://be.server.com/api/user/login. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 503.

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://be.server.com/api/user/login. (Reason: CORS request did not succeed). Status code: (null).

Error: [object Object]

0

There are 0 answers