Question

Routing node.js with express js

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: http://pastebin.com/J8fK77H8

For my other configurations for nginx, everything has been based from this guide here: https://code.lengstorf.com/deploy-nodejs-ssl-digitalocean/

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 Thanks, Michael

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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:

    "dev": "DEBUG=nodemon:* nodemon server.js",```
 and a nodemon config: `nodemon.json` that has all tracing on: 
```# cat nodemon.json
{
  "restartable": "rs",
  "ignore": [
    "*.md",
    "notes"
  ],
  "verbose": true,
  "execMap": {
    "js": "node"
  },
  "env": {
    "DEBUG": "*",
    "NODE_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.

Hello!

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

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/michaeluy.me/node_modules/express/lib/application.js:617:24)
    at Object.<anonymous> (/apps/michaeluy.me/server.js:30:5)
    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