1

I have search a lot and I can't seem to start express js app. All I'm getting is 404 error.

Default app.js file which has http server and it works fine.

var http = require('http');
var server = http.createServer(function(req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    var message = 'It works!\n',
        version = 'NodeJS ' + process.versions.node + '\n',
        response = [message, version].join('\n');
    res.end(response);
});
server.listen();

And this is express js code which is not working. giving me 404 error.

var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
server.listen();

I also tried few other combination as well.

var express = require('express');
var app = express();
app.get('/', (req, res) => res.send('Hello World!'));
var http = require('http');
var server = http.createServer(app);
server.listen();

this one also didn't work

var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
app.get('/', (req, res) => res.send('Hello World!'));
server.listen();

I also tried expressjs to create it own server and it also didn't work.

const express = require('express');
const app = express();
const port = 80;
app.get('/', (req, res) => res.send('Hello World!'));
app.listen(port, () => console.log(`Example app listening on port ${port}!`));

I also tried to remove port from app listen and not surprisingly it also didn't work.

const express = require('express');
const app = express();
app.get('/', (req, res) => res.send('Hello World!'));
app.listen();

I also tried everything from express-js-app-listen-vs-server-listen page but not successful.

This is the error I get

Expressjs 404

1 Answers1

0

For others with the same A2 Hosting issue, the solution is actually so easy...

The problem with the application running the NodeJS deployment on cPanel. Phusion Passenger does not use the root path as '/', but as '/yourAppURL'. So in your NodeJS code, you have to add the specified AppURLPath to the call when using Express...

e.g.

//Instead of 
app.get('/', (req, res) => res.send('Hello World!'));

//Change to
app.get('/YourSpesifiedAppURLPath/', (req, res) => res.send('Hello World!'));