Report this

What is the reason for this report?

Routing node.js with express js

Posted on November 12, 2016
Mayk

By Mayk

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



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!

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.

Hello!

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

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 /

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.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.