tombrokeoff (13) [Avatar] Offline
#1
Guys,

I am going through (what is in my version of the book) Chapter 8 and I am running into some issues and I think it has to do with the version of Express that I am running. Is the code in the book for the Photo app not compatible with Express 4.0.0? Do I need to revert back to 3? If so, whats the best way to do this? When I run the command express -e photo, the files I am left with differ from the code in the book.

Thanks,
Justin
tombrokeoff (13) [Avatar] Offline
#2
Re: Express - Photo Sharing App - problems
to further my original post .... once I use express to generate the skeleton files and then run npm install, I cant start the app by running node app.js (as stated in the book) I have to run npm start. Also, the app.js file that express generates for me looks like this:

var express = require('express');
var path = require('path');
var favicon = require('static-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', 'ejs');

app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

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

/// catch 404 and forwarding 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;
garthabrindoid (21) [Avatar] Offline
#3
Re: Express - Photo Sharing App - problems
I have a working version of the app zipped and available for download here:

https://cmu.box.com/s/cnzlwvkcv5x57o30mska

I made the necessary changes for Express 4.x, and it works fine for me (Mac OS 10.9, Node.js v0.10.26). Do this to get it running:

1) Download the zip and unzip to your favorite location
2) Open a terminal and cd to the directory created when you unzipped
3) Run "npm install"
4) Run the app with "npm start". Or, if you want debug statements, run it with "DEBUG=app,photos ./bin/www".

NOTE: I modified the ./bin/www script to run the app with nodemon. So, you'll need to have nodemon installed (npm install -g nodemon) for it to work. Or just edit the first line of the ./bin/www script to read "#!/usr/bin/env node".

Hope this helps.

Chris
tombrokeoff (13) [Avatar] Offline
#4
Re: Express - Photo Sharing App - problems
thanks Chris
minusthebear69 (3) [Avatar] Offline
#5
Re: Express - Photo Sharing App - problems
Thank you for this. I was banging my head against the wall trying to get the book's script to work.

I will study the script you wrote this week so I can understand what you did differently, but as a noob any pointers you could give on your code would extremely appreciated.
garthabrindoid (21) [Avatar] Offline
#6
Re: Express - Photo Sharing App - problems
I have little memory for code, so I don't remember a lot about it to be honest. I was new to Node at the time too, and pretty much just fumbled around until I got it working. I know more now, and would probably do things differently now, but wouldn't call myself an expert.

Anyway, let me know if you have specific questions and I'll see if I can remember why I did what I did. :-\
minusthebear69 (3) [Avatar] Offline
#7
Re: Express - Photo Sharing App - problems
So I came back to the Express photo folder you created to take a look at it again, and all of a sudden it doesn't work. I don't know if it's a mistake I'm making or something has changed in mongoose or express.

I type in DEBUG=app,photo ./bin/www as you suggest, and I get the following:

minusthebear@minusthebear-R410P:~/Desktop/photo$ DEBUG=app,photos ./bin/www
20 Sep 16:28:40 - [nodemon] v1.2.1
20 Sep 16:28:40 - [nodemon] to restart at any time, enter `rs`
20 Sep 16:28:40 - [nodemon] watching: *.*
20 Sep 16:28:40 - [nodemon] starting `node ./bin/www`
app View Cache Enabled = false +0ms
app Express server listening on port 3000 +8ms

events.js:72
throw er; // Unhandled 'error' event
^
Error: failed to connect to [localhost:27017]
at null.<anonymous> (/home/minusthebear/Desktop/photo/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:546:74)
at EventEmitter.emit (events.js:106:17)
at null.<anonymous> (/home/minusthebear/Desktop/photo/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:150:15)
at EventEmitter.emit (events.js:98:17)
at Socket.<anonymous> (/home/minusthebear/Desktop/photo/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:533:10)
at Socket.EventEmitter.emit (events.js:95:17)
at net.js:441:14
at process._tickCallback (node.js:415:13)
20 Sep 16:28:40 - [nodemon] app crashed - waiting for file changes before starting...



Maybe I need to change what Mongoose connects to? Currently I have it connecting to: mongoose.connect('mongodb://localhost/photo_app');

This all worked for me a couple months ago, and now it doesn't. I don't get it.
minusthebear69 (3) [Avatar] Offline
#8
Re: Express - Photo Sharing App - problems
Nevermind, figured it out. I needed to run MongoDB in a separate terminal.

Sorry to waste your time.