0

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?

Community
  • 1
  • 1
Mindsect Team
  • 2,311
  • 5
  • 29
  • 36
  • Where in your code did you place /about routing? – ezpn Sep 25 '15 at 21:58
  • @ezrepotein , I placed the code right after the 'app.use' routing commands and right before the 404 assignments. It seemed like the logical place to put them. However, now that you asked this particular question, perhaps I will try placing them in different parts of the code. Perhaps there I'm assigning a route too late or early in the code? – Mindsect Team Sep 26 '15 at 02:00
  • @ezrepotein, I have figured out my main issue. The book that I'm using for tutorials is a great book, however the version of Express used for the tutorials and download code is 3.12.0. So, I noticed that a ton of script, routing and methods are similar but coded differently. If I look at each app.js side by side, there are some distinct differences that are pulling errors. I will just utilize Express 3.12.0 until I'm finished with the principles of this book. – Mindsect Team Sep 27 '15 at 22:13

1 Answers1

1

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;
Alex da Silva
  • 4,552
  • 2
  • 17
  • 25
  • Thanks for the advice Alex. I noticed that I have been using Express 4 API documentation to understand the principles in a book that is using Express 3 techniques. As the challenges become more complex, the more errors are being triggered. I have since installed Express 3 and everything seems to be working fine. Thanks a million. – Mindsect Team Sep 27 '15 at 22:15
  • I'm going to mark your answer as the solution, as providing me with the minimal code for my app.js file allowed me to realize that with that minimum code, the only difference was the code I took from a book. After checking the package.json file for this book tutorial, the Express version appeared to be Express 3 and not 4. Thank you for the step in the right direction. – Mindsect Team Sep 27 '15 at 22:17