2

I am trying to implement a simple nodejs server with Express routing on A2 Shared Hosting service (once I get this working I can deploy a more complex app I have been working on).

The root returns a response (API Running) but I get a 503 error on any other routes I try setting up. I have gone through many postings but none seem to address my exact problem. The hosting service has an article suggesting using dynamic post assignment (which I have implemented).

If the root works but other routing doesn't, could this be something to do with EXPRESS (but again that works fine on localhost)?

The server.js file looks like this:

const express = require('express');
const snow = require('./routes/api/snow');

const app = express();

// Middle ware init
app.use(express.json({ extended: false }));

app.get('/snowshoe/', (req, res) => res.send('API Running'));

// Define routes
app.use('/snowshoe/api/snow', snow);

const server = app.listen(0, () => {
    console.log('Example app listening at http://localhost:', server.address().port);
});

app.use(function (req, res, next) {
  res.status(404).send("Sorry can't find that!")
})

and the /snowshoe/api/snow.js looks like this:

const express = require('express');
const router = express.Router();

router.get('/', (req, res) => res.send('Snow plow required'));

module.exports = router;

Perhaps something with the Express routing?

package.json

{
"name": "snowshoe",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
    "start": "node server"
},
"author": "",
"license": "ISC",
"dependencies": {
    "express": "^4.17.2"
}

}

Ken G
  • 21
  • 4
  • Actually, you are listening a random port. Is that intended ? – Adrien De Peretti Feb 04 '22 at 22:55
  • I used the suggestion in the hosting co documentation: https://www.a2hosting.com/kb/cpanel/cpanel-software/migrating-a-node-js-application-to-node-js-selector – Ken G Feb 05 '22 at 00:30
  • Without any further details on the hosting setup - where this issue probably arises (assuming you can run your app fine locally) - it is hard to help with this question. – Niels Abildgaard Feb 15 '22 at 12:04
  • Hi Niels and thank you for the comment. Its a simple shared hosting setup with Nodejs being run through a Cpanel app. I would seem that the Express routing doesnt work properly. It does work fine locally. I am adding my package.json above. – Ken G Feb 15 '22 at 15:15
  • Just to summarize, the server runs and the root /snowplow/ returns "API Running" but the express routing to /snowplow/api/snow gives the 503 error. – Ken G Feb 15 '22 at 15:30

1 Answers1

1

When you create a server (it's not just about node.js or express) you need to put a 4 digit number like 4000.

const port = 4000
const server = app.listen(port, () => {
   console.log('Example app listening at http://localhost:${port}')
});
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Yurikaza
  • 23
  • 1
  • 7
  • Actually the hosting service requires a dynamically assigned port number as per extract from instructions to migrate from local to hosting: Port number: If your existing application uses a hard-coded port number, you should change it to obtain a port number dynamically. – Ken G Feb 05 '22 at 11:22