Routing node.js with express js

Posted November 12, 2016 5k views

Hello everyone,

I am having a problem with my website. I am running Ubuntu 16.04.1 x64 with node.js. For my website, whenever I go to the other pages on my website. It directs them to the 404 page even though I have a routing set for it.

Here is my server.js file:

For my other configurations for nginx, everything has been based from this guide here:

It actually is my first time deploying a node.js app here so I am still not the best with it.

Hope someone can help here

1 comment
  • A couple questions.

    1.) Are you seeing your custom 404 page defined in your app or the default one from nginx?

    2.) Are you seeing a 404 for all pages including / or just for certain ones?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
4 answers

Quick answer, try starting your node server with DEBUG=express:* node server.js and watch what Express tells you is happening.

Try develop/test with script tags in package.json along following lines:
"start": "NODE_ENV=production DEBUG=*:error node server.js",
"dev": "DEBUG=nodemon:* nodemon server.js",

and a nodemon config: nodemon.json that has all tracing on:
# cat nodemon.json
"restartable": "rs",
"ignore": [
"verbose": true,
"execMap": {
"js": "node"
"env": {
"DEBUG": "*",
"NODE_ENV": "development"
"ext": "js, json, html"

NODE_DEBUG is likely too low level and you would only turn on one at a time NODE_DEBUG=cluster,net,http,fs,tls,module,timers

Probably not best way to do it, but create a logger per module(*.js file) (change myApp to match module) and you can do stuff like DEBUG=*:error,myRoutes:*,express:* … whatever you feel like.
appLog = { // logger
/* eslint-disable sort-keys */
err: debug("myApp:error"), // 0 -- a problem occured
warn: debug("myApp:warning"), // 1 -- this could be a problem
info: debug("myApp:info"), // 2 -- normal event
debug: debug("myApp:debug") // 3 -- use for debugging/development
/* eslint-enable sort-keys */

Not sure how well this would scale out and or behave in production… but you need some basic logging functionality. Never built real production apps.

Hi ryanpq,

  1. I am seeing the 404 page as included on my app

  2. I get redirected to all the 404 page to all other pages except /

I have tried this out and it actually turned out to have an error like this.

Error: listen EADDRINUSE :::3000
    at Object.exports._errnoException (util.js:1026:11)
    at exports._exceptionWithHostPort (util.js:1049:20)
    at Server._listen2 (net.js:1257:14)
    at listen (net.js:1293:10)
    at Server.listen (net.js:1389:5)
    at EventEmitter.listen (/apps/
    at Object.<anonymous> (/apps/
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)

I then changed 3000 to 5000 on my server.js file as shown here:

app.listen(5000, function () {
    console.log('Listening on port 5000!');

and now. It works perfectly! Thank you very much


FYI: Error: listen EADDRINUSE :::3000 means that the port is already in use! So someone is already running an app using 3000.