Reputation: 213
Just getting my feet wet with Node.js and Backbone.js. I'm using the book "Backbone Blueprints" and some of the code he provides to set up the webserver appears to not work.
I have Node.js installed and running (I know this works). His package.json code seemed to do the trick, but I'll post it below just in case:
{
"name": "simple-blog",
"description": "This is a simple blog.",
"version": "0.1.0",
"scripts": {
"start": "nodemon server.js"
},
"dependencies": {
"express": "3.x.x",
"ejs": "~0.8.4",
"bourne": "0.3"
},
"devDependencies": {
"nodemon": "latest"
}
}
This is the server.js code that errors out when I try to turn on the server:
var express = require('express');
var path = require('path');
var Bourne = require("bourne");
var app = express();
var posts = new Bourne("simpleBlogPosts.json");
var comments = new Bourne("simpleBlogComments.json");
app.configure(function(){
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
});
app.get('/*', function (req, res) {
res.render("index.ejs");
});
app.listen(3000);
Full error:
> nodemon server.js
11 Mar 19:35:22 - [nodemon] v1.3.7
11 Mar 19:35:22 - [nodemon] to restart at any time, enter `rs`
11 Mar 19:35:22 - [nodemon] watching: *.*
11 Mar 19:35:22 - [nodemon] starting `node server.js`
undefined:0
^
SyntaxError: Unexpected end of input
at Object.parse (native)
at new Bourne (C:\Users\MyName\WebstormProjects\simpleBlog\node_modules\bou
rne\lib\bourne.js:52:30)
at Object.<anonymous> (C:\Users\MyName\WebstormProjects\simpleBlog\server.j
s:6:13)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
I did digging before I decided to post this question. Comments elsewhere indicate this can happen if you're attempting to use an undefined/empty variable or if you're missing a '{' somewhere.
That doesn't appear to be the case with this code, and this is literally straight out of the book.
Webstorm does note that the app.configure function has an 'invalid number of arguments' and that it is 'expecting 2'.
The book itself is not old at all, having come out in 2014.
Just in case: this is not a 'homework' question, I'm trying to teach myself Backbone.js and it just so happens the technologies this author chose to facilitate that learning quest consisted of Node.js and Express among others.
Thanks in advance!
Upvotes: 1
Views: 3224
Reputation: 288090
The problem is that bourne expects simpleBlogPosts.json
to contain a valid JSON document, but the file does not.
Simply deleting simpleBlogPosts.json
and restarting the server should be enough to generate a valid JSON file, i.e. with contents []
. Most likely, you need to do the same with simpleBlogComments.json
.
Upvotes: 1