Just incase anyone else runs into the same problem, what seemed to be happening was that the express generator was creating a project that was based on a newer version of express. Hence the file structure and file contents were totally different and following the instructions in the book was just breaking stuff.
I’m not quite sure why that was the case. Mybe a previous version of exxpress got installed when I was first messing around with node. All my efforts using npm seemed to show that I had express 2.5.8 installed globally, but the project generated seemed to be based on express 4.
Specifically, the book tells you to:
1 - install express 2.5.8 globally.
2 - use “express authentication” to generate the project
3 - edit the project.json file in the new directory and then run “npm install”
But I had to:
1 - install express 2.5.8 globally.
2 -manually create the project directory and copy in the project.json. (which specifies express 2.5.8)
3 - run “npm install”
otherwise I got the wrong file structure.
I’m a little worried that the file structure the book is based on and the file structure created by the latest version are so totally different… I’m hoping that’s just a difference in express, not node as a whole, and that it doesn’t mean I’m wasting time learning outdated code.
The app.js that express was generating for me (that was causing problems):
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: {}
});
});
module.exports = app;
The book’s code sample app.js (that works on 2.5.8):
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, fs = require('fs')
, User = require('./models/User.js');
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', routes.index);
app.get('/form', function(req, res) {
fs.readFile('./form.html', function(error, content) {
if (error) {
res.writeHead(500);
res.end();
}
else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(content, 'utf-8');
}
});
});
app.post('/signup', function(req, res) {
var username = req.body.username;
var password = req.body.password;
User.addUser(username, password, function(err, user) {
if (err) throw err;
res.redirect('/form');
});
});
app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);