Grateful Grateful - 2 months ago 18
Node.js Question

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

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

require
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

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.

Comments