Mindsect Team
Mindsect Team

Reputation: 2571

Express Routing with Standard Express.js 4 (generator) Installation Not Working

My question is less about production code and more of an issue with official documentation for someone who wants to learn a new script or language.

After installing express-generator (0.12.2) and express (4.13.1), I am able to run the following successfully create a new project by running the following command:

npm express example

The example directory is now on my drive and I then change directories and install all default dependencies from the package.JSON file:

cd example && npm install

No problems so far.

I open the app.js file located in the root of my 'example' directory and the Express 4 version of the app.js is as follows:

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 app = express();

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

// 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);

// 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: {}
      });
});

app.listen(3000)
module.exports = app;

After adding the app.listen(3000); line (second from last line) as learned from a previous thread (Starting Express.js Using app.js or 'npm start'?), running the following command allows me to server the example directory:

node app.js

All is well. However, after visiting the Express documentation, also for version 4 (http://expressjs.com/guide/routing.html), I see the following instructions:

Route paths Route paths, in combination with a request method, define the endpoints at which requests can be made to. They can be strings, string patterns, or regular expressions. Examples of route paths based on strings:

// will match request to the root
app.get('/', function (req, res) {
  res.send('root');
});

// will match requests to /about
app.get('/about', function (req, res) {
  res.send('about');
});

// will match request to /random.text
app.get('/random.text', function (req, res) {
  res.send('random.text');
});

After adding the following line from the Express 4 documentation:

// will match requests to /about
app.get('/about', function (req, res) {
  res.send('about');
});

And visiting the following URL:

http://127.0.0.1:3000/about

I receive a 404 not found error, along with an error at line 30, which is:

28 // application routes
29 app.post('/', function(req, res, next) {
30  res.send(req.body);
31 });

Any ideas?

Upvotes: 0

Views: 803

Answers (1)

Alex da Silva
Alex da Silva

Reputation: 4590

If you added only the code below after app.use like you said, your code should work:

// will match requests to /about
app.get('/about', function (req, res) {
  res.send('about');
});

You probably add more code and that could be the problem. Hint: I don't see app.post in your original code.

Here is the code with new app.get for /about route that works:

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 app = express();

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

// 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);

// will match requests to /about
app.get('/about', function (req, res) {
  res.send('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: {}
  });
});

app.listen(3000);
module.exports = app;

Upvotes: 1

Related Questions