Thinkster MEAN Stack教程,使用expressJS路由/设置REST apis时出错

时间:2016-12-22 07:00:44

标签: javascript node.js mongodb express mean-stack

我目前正在关注this tutorial创建一个MEAN堆栈项目。在项目的这个阶段,我使用Express在Mongo数据库上打开休息路径。这是我添加到/routes/index.js顶部的代码(使用express --ejs自动处理)。

var mongoose = require('mongoose');
var Post = mongoose.model('Post');

当我尝试使用npm start运行服务器时,npm以退出状态1退出,而./bin/www初始脚本失败。这是调试日志以及/routes/index.js的代码和创建Post模式的文件/models/Posts.js。有谁知道如何解决这个问题?如果您需要更多信息,请告诉我们。

./路由/ index.js

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Post = mongoose.model('Post');
//var Comment = mongoose.model('Comment');

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

module.exports = router;

./模型/ Posts.js

var mongoose = require('mongoose');
var PostSchema = new mongoose.Schema({
  title: String,
  link: String,
  upvotes: {type: Number, default: 0},
  comments: [{type: mongoose.Schema.Types.ObjectId, ref: 'Comment'}]
});

mongoose.model('Post', PostSchema);

NPM-的debug.log

0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/nodejs', '/usr/bin/npm', 'start' ]
2 info using npm@3.10.10
3 info using node@v7.2.1
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle bluenews@0.0.0~prestart: bluenews@0.0.0
6 silly lifecycle bluenews@0.0.0~prestart: no script for prestart,continuing
7 info lifecycle bluenews@0.0.0~start: bluenews@0.0.0
8 verbose lifecycle bluenews@0.0.0~start: unsafe-perm in lifecycle true
9 verbose lifecycle bluenews@0.0.0~start: PATH: /usr/lib/node_modules/npm/bin/node-gyp-bin:/home/user/Dropbox/Coding/redditClone/bluenews/node_modules/.bin:/usr/local/heroku/bin:/home/user/anaconda2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
10 verbose lifecycle bluenews@0.0.0~start: CWD: /home/user/Dropbox/Coding/redditClone/bluenews
11 silly lifecycle bluenews@0.0.0~start: Args: [ '-c', 'node ./bin/www' ]
12 silly lifecycle bluenews@0.0.0~start: Returned: code: 1  signal: null
13 info lifecycle bluenews@0.0.0~start: Failed to exec start script
14 verbose stack Error: bluenews@0.0.0 start: `node ./bin/www`
14 verbose stack Exit status 1
14 verbose stack     at EventEmitter.<anonymous> (/usr/lib/node_modules/npm/lib/utils/lifecycle.js:255:16)
14 verbose stack     at emitTwo (events.js:106:13)
14 verbose stack     at EventEmitter.emit (events.js:191:7)
14 verbose stack     at ChildProcess.<anonymous> (/usr/lib/node_modules/npm/lib/utils/spawn.js:40:14)
14 verbose stack     at emitTwo (events.js:106:13)
14 verbose stack     at ChildProcess.emit (events.js:191:7)
14 verbose stack     at maybeClose (internal/child_process.js:885:16)
14 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)
15 verbose pkgid bluenews@0.0.0
16 verbose cwd /home/user/Dropbox/Coding/redditClone/bluenews
17 error Linux 4.4.0-53-generic
18 error argv "/usr/bin/nodejs" "/usr/bin/npm" "start"
19 error node v7.2.1
20 error npm  v3.10.10
21 error code ELIFECYCLE
22 error bluenews@0.0.0 start: `node ./bin/www`
22 error Exit status 1
23 error Failed at the bluenews@0.0.0 start script 'node ./bin/www'.
23 error Make sure you have the latest version of node.js and npm installed.
23 error If you do, this is most likely a problem with the bluenews package,
23 error not with npm itself.
23 error Tell the author that this fails on your system:
23 error     node ./bin/www
23 error You can get information on how to open an issue for this project with:
23 error     npm bugs bluenews
23 error Or if that isn't available, you can get their info via:
23 error     npm owner ls bluenews
23 error There is likely additional logging output above.
24 verbose exit [ 1, true ]

1 个答案:

答案 0 :(得分:0)

您需要导出&#39;发布&#39;在Post.js文件中,只有它对其他模块可见。

在您的情况下,您必须执行以下步骤

var Post=mongoose.model('Post', PostSchema);
module.exports=Post;

然后您需要导入此文件,如下所示

 var Post = mongoose.model('./models/Posts.js');

您还需要在index.js文件中包含路径模块 例如:

 var path = require('path');
行var express = require(&#39; express&#39;)之后; var router = express.Router();