Grateful Grateful - 1 year ago 91
Node.js Question

Within express routes, why have `./routes` instead of `/routes`?

Almost every example I have seen, the app.js is using

with the path
. I am wondering why can't we just use
. So for example, why can't we do the following.

var express = require('express');
var app = express();
var routes = require('/routes');

Answer Source

You can use require('/routes');, but that would look for the routes file at the top level of your disk volume (in the root directory). So, if the current volume is //mydisk, then require('/routes'); would look for the file at //mydisk/routes.

The reason people use require('./routes') is to load the routes files from the current directory, not from the top level of the current volume.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download