Node.js – 未定义:0语法错误:意外的input结束

用Node.js和Backbone.js来弄湿我的脚。 我正在使用“Backbone Blueprints”这本书,他提供的一些代码来设置networking服务器似乎不起作用。

我有Node.js安装和运行(我知道这个工程)。 他的package.json代码似乎有诀窍,但我会在下面发布它以防万一:

{ "name": "simple-blog", "description": "This is a simple blog.", "version": "0.1.0", "scripts": { "start": "nodemon server.js" }, "dependencies": { "express": "3.xx", "ejs": "~0.8.4", "bourne": "0.3" }, "devDependencies": { "nodemon": "latest" } } 

这是我试图打开服务器时出错的server.js代码:

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

完整的错误:

 > 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 

我决定发表这个问题之前,我做了挖掘。 其他地方的注释表明,如果您尝试使用未定义的/空variables或者在某个地方缺less“{”,则可能发生这种情况。

这段代码似乎并不是这样,而这本书就是直接的。

Webstorm确实注意到,app.configure函数有一个“无效的参数数量”,而且是“期待2”。

这本书本身并不老,于2014年问世。

以防万一:这不是一个“家庭作业”的问题,我试图教自己Backbone.js,而且这个作者select促进这个学习任务的技术是由Node.js和Express等组成的。

提前致谢!

问题是bourne期望simpleBlogPosts.json包含一个有效的JSON文档,但文件不包含。

简单地删除simpleBlogPosts.json并重新启动服务器应该足以生成一个有效的JSON文件,即simpleBlogPosts.json [] 。 最有可能的是,您需要对simpleBlogComments.json执行相同的simpleBlogComments.json