beginning-backbone
beginning-backbone copied to clipboard
Running the node.js server
The book is awesome. All examples utilizing the node server have issues though. Every time I install all dependencies and run the file I get a 'Cannot GET /' There are no javascript errors reported, just a blank page with the above text on it. The code you've presented is great and modular enough to pull out and try with working connections, but it would be cool if the code ran just out of the box.
When I try to run this I get the following error:
/Users/david/Sites/apress/server.js:28
app.configure(function () {
^
TypeError: Object function (req, res, next) {
app.handle(req, res, next);
} has no method 'configure'
at Object.
The fix is easy: just change "configure" to "set".
Its a shame a book as good as this has broken code. I neither was able to get the server to work. changinf set to configure didn't work for me.
It would be great if the author update the server.js code for express 4.x
Here is the server.js code that worked for me.
/**
* A simple API hosted under localhost:8080/books
*/
var express = require('express');
var app = express();
var bookId = 100;
app.get('/', function (req, res) {
res.send('Hello World!');
});
function findBook(id){
for(var i =0; i < books.length; i++){
if(books[i].id === id){
return books[i];
}
}
return null;
}
function removeBook(id){
var bookIndex = 0;
for(var i=0; i < books.length; i++){
if(books[i].id === id){
bookIndex = i;
}
}
books.splice(bookIndex, 1);
}
var books = [
{id: 98, author: 'Stephen King', name: 'The Shining', year: 1977},
{id: 99, author: 'George Orwell', name: '1984', year: 1949}];
/**
* HTTP GET /books
* Should return a list of books
*/
app.get('/books', function (request, response) {
response.header('Access-Control-Allow-Origin', '*');
console.log('In GET function ');
response.json(books);
});
/**
* HTTP GET /books/:id
* id is the unique identifier of the book you want to retrieve
* Should return the task with the specified id, or else 404
*/
app.get('/books/:id', function (request, response) {
response.header('Access-Control-Allow-Origin', '*');
console.log('Getting a book with id ' + request.params.id);
var book = findBook(parseInt(request.params.id,10));
if(book === null){
response.send(404);
}
else{
response.json(book);
}
});
/**
* HTTP POST /books/
* The body of this request contains the book you are creating.
* Returns 200 on success
*/
app.post('/books/', function (request, response) {
response.header('Access-Control-Allow-Origin', '*');
var book = request.body;
console.log('Saving book with the following structure ' + JSON.stringify(book));
book.id = bookId++;
books.push(book);
response.send(book);
});
/**
* HTTP PUT /books/
* The id is the unique identifier of the book you wish to update.
* Returns 404 if the book with this id doesn't exist.
*/
app.put('/books/:id', function (request, response) {
response.header('Access-Control-Allow-Origin', '*');
var book = request.body;
console.log('Updating Book ' + JSON.stringify(book));
var currentBook = findBook(parseInt(request.params.id,10));
if(currentBook === null){
response.send(404);
}
else{
//save the book locally
currentBook.title = book.title;
currentBook.year = book.year;
currentBook.author = book.author;
response.send(book);
}
});
/**
* HTTP DELETE /books/
* The id is the unique identifier of the book you wish to delete.
* Returns 404 if the book with this id doesn't exist.
*/
app.delete('/books/:id', function (request, response) {
console.log('calling delete');
response.header('Access-Control-Allow-Origin', '*');
var book = findBook(parseInt(request.params.id,10));
if(book === null){
console.log('Could not find book');
response.send(404);
}
else
{
console.log('Deleting ' + request.params.id);
removeBook(parseInt(request.params.id, 10));
response.send(200);
}
response.send(200);
});
//additional setup to allow CORS requests
var allowCrossDomain = function(req, response, next) {
response.header('Access-Control-Allow-Origin', "http://localhost");
response.header('Access-Control-Allow-Methods', 'OPTIONS, GET,PUT,POST,DELETE');
response.header('Access-Control-Allow-Headers', 'Content-Type');
if ('OPTIONS' == req.method) {
response.send(200);
}
else {
next();
}
};
var server = app.listen(8080, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});