Node.js -- Undefined:0 Syntax Error: Unexpected end of input

Node.js -- Undefined:0 Syntax Error: Unexpected end of input

Node.js 和 Backbone.js 刚弄湿我的脚。我正在使用 "Backbone Blueprints" 这本书,他提供的一些用于设置网络服务器的代码似乎无法正常工作。

我安装了 Node.js 和 运行(我知道这有效)。他的 package.json 代码似乎可以解决问题,但我将 post 放在下面以防万一:

{
  "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"
  }
}

这是 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

在决定 post 这个问题之前,我进行了挖掘。其他地方的评论表明,如果您尝试使用 undefined/empty 变量或者您在某处缺少“{”,就会发生这种情况。

这段代码似乎并非如此,这实际上是直接从书中摘录的。

Webstorm 注意到 app.configure 函数有一个 'invalid number of arguments',它是 'expecting 2'。

这本书本身一点也不旧,2014年出版的。

以防万一:这不是一个 'homework' 问题,我正在尝试自学 Backbone.js 碰巧作者选择了促进学习任务的技术包括 Node.js 和 Express 等等。

提前致谢!

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

只需删除 simpleBlogPosts.json 并重新启动服务器就足以生成有效的 JSON 文件,即内容为 []。很可能,您需要对 simpleBlogComments.json.

执行相同的操作