Happydevdays Happydevdays - 1 year ago 62
Node.js Question

nodejs / express - fresh install fails when attempting to navigate to routes

I just installed node and express on a new dev server...
and I created a test app by running the following command:

dev-server15:/var/www# express mytest

create : mytest
create : mytest/package.json
create : mytest/app.js
create : mytest/public
create : mytest/public/javascripts
create : mytest/public/images
create : mytest/public/stylesheets
create : mytest/public/stylesheets/style.css
create : mytest/routes
create : mytest/routes/index.js
create : mytest/routes/users.js
create : mytest/views
create : mytest/views/index.jade
create : mytest/views/layout.jade
create : mytest/views/error.jade
create : mytest/bin
create : mytest/bin/www

install dependencies:
$ cd mytest && npm install

run the app:
$ DEBUG=mytest:* npm start

dev-server15:/var/www# cd mytest/
dev-server15:/var/www/mytest# npm install

The npm install command completes without any errors.
I start the application by doing the following:

dev-server15:/var/www/mytest# DEBUG=mytest:* npm start

> mytest@0.0.0 start /var/www/mytest
> node ./bin/www

mytest Listening on port 3000 +0ms

Then i try to navigate to the index page by going to http://mydevserver15/test

I can see the requests being made in the console... but it returns a 404.

dev-server15:/var/www/mytest# DEBUG=mytest:* npm start

> mytest@0.0.0 start /var/www/mytest
> node ./bin/www

mytest Listening on port 3000 +0ms
GET /mytest 404 734.109 ms - 965
GET /stylesheets/style.css 200 12.556 ms - 111

I thought out of the box, the default routes worked.

What I've checked:

I know that I need references to my routes in the app.js file.
So I've tried to check and I do see that I have the following entries:

var routes = require('./routes/index');
var users = require('./routes/users');

and then later on, I see:

app.use('/', routes);
app.use('/users', users);

Here's what i have in package.json so you can see the version numbers:

dev-server15:/var/www/mytest# cat package.json
"name": "mytest",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
"dependencies": {
"body-parser": "~1.15.1",
"cookie-parser": "~1.4.3",
"debug": "~2.2.0",
"express": "~4.13.4",
"jade": "~1.11.0",
"morgan": "~1.7.0",
"serve-favicon": "~2.3.0"


Here's the contents of routes/index.js:

dev-server15:/var/www/mytest# cat routes/index.js
var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });


I also tried navigating to the users route. I've copied and pasted the url for you here, as well as the output on the console:


This creates the following output in the console:

GET /mytest/users 404 74.173 ms - 965
GET /stylesheets/style.css 304 2.851 ms - -

Answer Source

You have one route: "/", this mean that you should navigate to mydevserver15 to see root page. If you want page /test, you should change:

router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });


router.get('/test', function(req, res, next) {
  res.render('index', { title: 'Express' });

Or register it as new route.

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