我的/router/index.js
文件开始变得非常拥挤,我希望将我的路由按组(用户路线,发布路线,装备路线)分成/router/routes/
中的自己的文件。这是我目前的工作:
app.js
var express = require('express');
var fs = require('fs');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var passport = require('passport');
// database connection
require('./models/posts');
require('./models/Comments');
require('./models/Users');
require('./models/Gear');
//mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/news');
var router = require('./router');
var app = express();
app.use('/', router);
// login setup
require('./config/passport');
app.use(passport.initialize());
// logger
// create a write stream (in append mode)
var accessLogStream = fs.createWriteStream(__dirname + '/access.log', {flags: 'a'})
// setup the logger
app.use(logger('combined', {stream: accessLogStream}))
app.use(logger('dev'));
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// TODO: add favicon
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// don't print stacktraces for user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
/router/index.js
var express = require('express');
var fs = require('fs');
var router = express.Router();
var mongoose = require('mongoose');
var Post = mongoose.model('Post');
var Comment = mongoose.model('Comment');
var Gear = mongoose.model('Gear');
var passport = require('passport');
var jwt = require('express-jwt');
var User = mongoose.model('User');
var auth = jwt({secret: 'SECRET', userProperty: 'payload'});
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
/*
* Posts routes
*/
/* GET all posts */
router.get('/posts', function(req, res, next) {
Post.find(function(err, posts) {
if (err) return next(err);
res.json(posts);
})
});
/* POST add new post */
router.post('/posts', auth, function(req, res, next) {
var post = new Post(req.body);
post.author = req.payload.username;
post.save(function(err, post) {
if (err) return next(err);
res.json(post);
})
});
/* preload post */
router.param('post', function(req, res, next, id) {
var query = Post.findById(id);
query.exec(function(err, post) {
if(err) return next(err);
if (!post) return next(new Error('can\'t find post'));
req.post = post;
return next();
})
});
/* GET single post */
router.get('/posts/:post', function(req, res, next) {
req.post.populate('comments', function(err, post) {
if (err) { return next(err); }
res.json(post);
});
});
/* PUT upvote a post */
router.put('/posts/:post/upvote', auth, function(req, res, next) {
req.post.upvote(function(err, post) {
if (err) return next(err);
res.json(post);
})
});
/* PUT downvote a post */
router.put('/posts/:post/downvote', auth, function(req, res, next) {
req.post.downvote(function(err, post) {
if (err) return next(err);
res.json(post);
})
});
/*
* Comment routes
*/
/* POST comment on single post */
router.post('/posts/:post/comments', auth, function(req, res, next) {
var comment = new Comment(req.body);
comment.post = req.post;
comment.author = req.payload.username;
comment.save(function(err, comment){
if(err){ return next(err); }
req.post.comments.push(comment);
req.post.save(function(err, post) {
if(err){ return next(err); }
res.json(comment);
});
});
});
/* preload comment */
router.param('comment', function(req, res, next, id) {
var query = Comment.findById(id);
query.exec(function(err, comment) {
if(err) return next(err);
if (!comment) return next(new Error('can\'t find comment'));
req.comment = comment;
return next();
})
});
/* GET single comment */
router.get('/posts/:post/comments/:comment', function(req, res) {
res.json(req.comment);
});
/* PUT upvote comment on a post */
router.put('/posts/:post/comments/:comment/upvote', auth, function(req, res, next) {
req.comment.upvote(function(err, comment) {
if (err) return next(err);
res.json(comment);
})
});
/* PUT downvote a comment on a post */
router.put('/posts/:post/comments/:comment/downvote', auth, function(req, res, next) {
req.comment.downvote(function(err, comment) {
if (err) return next(err);
res.json(comment);
})
});
/*
* User routes
*/
/* POST Create User */
router.post('/register', function(req, res, next) {
if(!req.body.username || !req.body.password) {
return res.status(400).json({message: "Please fill out all fields"});
}
var user = new User();
user.username = req.body.username;
user.setPassword(req.body.password);
user.save(function(err) {
if (err) return next(err);
return res.json({token: user.generateJWT()})
});
});
/* POST login page */
router.post('/login', function(req, res, next){
if(!req.body.username || !req.body.password){
return res.status(400).json({message: 'Please fill out all fields'});
}
passport.authenticate('local', function(err, user, info){
if(err){ return next(err); }
if(user){
return res.json({token: user.generateJWT()});
} else {
return res.status(401).json(info);
}
})(req, res, next);
});
/*
* Gear routes
*/
/* GET all gear */
router.get('/gear', function(req, res, next) {
Gear.find(function(err, gear) {
if (err) return next(err);
res.json(gear);
})
});
/* preload gear item */
router.param('item', function(req, res, next, id) {
var query = Gear.findById(id);
query.exec(function(err, item) {
if(err) return next(err);
if (!item) return next(new Error('can\'t find item'));
req.item = item;
return next();
})
});
/* GET single gear item */
router.get('/gear/:item', function(req, res, next) {
req.item.populate('item', function(err, item) {
if (err) { return next(err); }
res.json(item);
});
});
/* POST add new gear */
router.post('/gear', auth, function(req, res, next) {
var gear = new Gear(req.body);
gear.owner = req.payload.username;
gear.save(function(err, gear) {
if (err) return next(err);
res.json(gear);
})
});
/* DELETE single gear item */
router.delete('/gear/:item/delete', auth, function(req, res, next) {
var query = Gear.findById(req.item, function(err, item) {
if (err) { return next(err) };
item.remove();
}).exec();
return res.json();
});
module.exports = router;
我尝试了几种不同的方式将所有posts
路由放入/router/routes/post-routes.js
,如下所示:
var express = require('express');
var router = express.Router();
var jwt = require('express-jwt');
var auth = jwt({secret: 'SECRET', userProperty: 'payload'});
/*
* Posts routes
*/
/* GET all posts */
router.get('/posts', function(req, res, next) {
Post.find(function(err, posts) {
if (err) return next(err);
res.json(posts);
})
});
/* POST add new post */
router.post('/posts', auth, function(req, res, next) {
var post = new Post(req.body);
post.author = req.payload.username;
post.save(function(err, post) {
if (err) return next(err);
res.json(post);
})
});
/* preload post */
router.param('post', function(req, res, next, id) {
var query = Post.findById(id);
query.exec(function(err, post) {
if(err) return next(err);
if (!post) return next(new Error('can\'t find post'));
req.post = post;
return next();
})
});
/* GET single post */
router.get('/posts/:post', function(req, res, next) {
req.post.populate('comments', function(err, post) {
if (err) { return next(err); }
res.json(post);
});
});
/* PUT upvote a post */
router.put('/posts/:post/upvote', auth, function(req, res, next) {
req.post.upvote(function(err, post) {
if (err) return next(err);
res.json(post);
})
});
/* PUT downvote a post */
router.put('/posts/:post/downvote', auth, function(req, res, next) {
req.post.downvote(function(err, post) {
if (err) return next(err);
res.json(post);
})
});
module.exports = router;
/router/index.js
中的以下内容:
module.exports = function(app) {
var routeFiles = fs.readdirSync(__dirname)
routeFiles.forEach(function(file) {
if (file === "index.js" || file.substr(file.lastIndexOf('.') + 1) !== 'js')
return;
var name = file.substr(0, file.indexOf('.'));
require('./' + name)(app);
});
}
此时,我只是不知道自己错过了什么。我的目标文件结构是:
app.js
/router
- index.js
/routes
- post-routes.js
- user-routes.js
- comment-routes.js
答案 0 :(得分:1)
如果那是你的/router/index.js,那么你需要
var path = require('path')
// ...
var routeFiles = fs.readdirSync(path.join(__dirname, 'routes'))
而不是
var routeFiles = fs.readdirSync(__dirname)
否则您将读取错误的目录列表(/ router而不是/ router / routes)。
您还需要适当更改路线的require()
,以便路径正确('./routes/' + name
)。
最后,您的./router/routes/文件只是导出路由器而不是导出带有app
的函数,这不是您的./router/index.js所期望的。所以你可以做两件事之一:
更改每个./router/routes/文件的导出,以导出将其路由器添加到app
的函数:
module.exports = function(app) {
app.use(router);
};
将./router/index.js文件更改为不调用模块'导出为函数,而是将导出装载到app
:
app.use(require('./routes/' + name));