Thinkster MEAN Stack 教程,routing/setting 使用 expressJS 启动 REST api 时出错
Thinkster MEAN Stack tutorial, error when routing/setting up REST apis with expressJS
我目前正在关注 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。有谁知道如何解决这个问题?如果您需要更多信息,请告诉我。
./routes/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;
./models/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 ]
您需要在 Post.js 文件中导出 'Post',然后只有它对其他模块可见。
对于您的情况,您必须执行以下步骤
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('express');
var router = express.Router();
我目前正在关注 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。有谁知道如何解决这个问题?如果您需要更多信息,请告诉我。
./routes/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;
./models/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 ]
您需要在 Post.js 文件中导出 'Post',然后只有它对其他模块可见。
对于您的情况,您必须执行以下步骤
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('express'); var router = express.Router();