JumpStart node.js (sitepoint book) code still valid?

I just started working through Sitepoint’s book: Jumpstart Node.js and I’m having trouble with all the exercises in Chapter 1. (using express to do basic authentication and connect to a mongolab database).

The book asks to install a very specific version of Express. Then to use express to generate a project. Then to edit the project.json to require only some specific version of mongoose, etc… then to run “node install”.

But if I follow those instructions then I get lots of install errors.If I manage to get them installed and run app.js then I get other errors.
If I don’t specify those specific versions and install the latest versions then I can get the express server running, but the next step is to modify app.js to add some routes… but the app.js that is generated by newer versions of express seems to bear little relation to the one in the book. (Routes seem to be defined in another location, for example).

So, before I spend time on this book, can anyone (if they have it) confirm whether it’s still relevant or outdated, and whether they can get the first express authentication project installed and running using the instructions in chapter 1?

You can check out the first chapter free online here, and the part I’m having trouble with is everything from installing express onwards:
https://www.sitepoint.com/premium/books/nodejs1/online/ch01s02.html#d5e352
they also have the code online here:
https://github.com/spbooks/NODEJS1/

It’s a shame it’s not working, as it seems like an interesting example. I don’t mind if it’s a little outdated as long as it’s still relevant and useful for learning.
It might be an issue on my end, but everything else seems to be working ok.

Hey @tom_dot, our teaching assistant @bodrovis should be able to help you out.

Hi there,

I just followed the first chapter of the book and everything worked (almost) as expected for me.

There were a couple of deprecation warning along the way (which are easily fixed) and I can see how a couple of the steps might be a little confusing, but essentially everything should work just fine.

The book is definitely still relevant and Node.js is definitely a technology worth learning.

So how about we go through this step for step and see where you’re having trouble. Does that sound like a plan?

That would be great. Maybe it’s something simple.
I’m not at my home PC right now so give me a while and I’ll get back to you. Much appreciated.

I went back totally from scratch and reinstalled node etc…
It seems to be working now. I wonder if I had previously installed a different version of some of the npm modules and there was some kind of clash.

There seem to be a few differences from the book, particularly as the book seems to be assuming linux, but hopefully I can work out those differences as I go.

Thanks for the offer of help! I may well need it in future!

1 Like

No problem : )

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);
2 Likes

Hey,

Thanks for taking the time to come back and let us know what wasn’t working for you.

I’m afraid that’s the problem with books based on a fast moving technology — by the time the book comes out, things have moved on somewhat. What I tend to do in this case is install the newest version of everything and when stuff breaks, try to figure out why (and repair it). This offers quite a high learning effect.

Looking at the express page on npm, it seems the latest version is 4.13.3, so you can expect breaking changes from 2.5.8.

If you get stuck on anything else, then just let us know.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.