1

I have this about.js route and it works fine but I don't understand how / in router.get() would work while /about wouldn't?

var express = require('express');
var router = express.Router();

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

module.exports = router;

----------------- UPDATE ----------------------

It's basically what I got out of the box after installing express.js except the about lines.

I expected router.get('/about' ...) in about.js would work but it threw an error and it worked with / instead and that's what bugs me.

app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

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

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

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

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});


module.exports = app;
6
  • Could you explain a little better, please? Commented Sep 29, 2015 at 13:10
  • need more explaination Commented Sep 29, 2015 at 13:11
  • I've edited my question slightly. Basically I understand how / works for index.js but not for other routes. Commented Sep 29, 2015 at 13:17
  • 1
    What do you mean by /about not working? If you want the function to run when you visit http://yourpage.tld/about, you of course need a route for that: router.get('/about', function(req, res, next){ /* your code */});. Commented Sep 29, 2015 at 13:23
  • That's what I thought but I get error when I do that. Instead / would work when visiting http://example.com/about Commented Sep 29, 2015 at 13:27

2 Answers 2

1

The problem

When you define a route on your app.js as you did with app.use('/about', about);. You are already telling express that you expect requests to hit http://yourserver/about route.

If you try to define /about again inside your about.js with:

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

What you're doing is tellig the Express that you will hit a /about inside your firstly declared /about. So it will expect requests on this route: http://yourserver/about/about

The solution

It's actually what you're using. Define a root route inside your about.js as:

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

This way your app will be:

  • Modular since you're using different files for different routes (about.js, users.js)
  • Easier to read
  • With simplier routes inside each file, since you don't need to type /about everytime you wish to create a new route.

If you wish a http://yourserver/about/help simply add a handler inside your route file about.js as here:

router.get('/help', function(req, res, next) {
  res.render('help', { title: 'Help' });
});
Sign up to request clarification or add additional context in comments.

Comments

0

If you want the route /about work then you have to create another route:

router.get('/about', function(req, res, next) {
  res.render('about-page', { title: 'About' });
});

because / will only work for the home page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.