[英]Express js:multiple route files into single file
In my express.js application i am organizing my routes in following way 在我的express.js应用程序中,我按照以下方式组织路线
routes/comment.js(handle all the comment routes) 路由/comment.js(处理所有评论路由)
var express = require('express');
var router = express.Router();
var comment = require('../controller/comment');
router.route('/new').get(comment.create);
module.exports = router;
routes/post.js(handle all the post routes) route / post.js(处理所有发布路线)
var express = require('express');
var router = express.Router();
var post = require('../controller/post');
router.route('/new').get(post.create);
module.exports = router;
And i am including in app.js file like this way 我以这种方式将其包含在app.js文件中
//need to include these declarations into another file and include that file here
//eg: require('config/main-routes');
/*
routes like comment/new
*/
var comment = require('./routes/comment');
app.use('/comment',comment);
/*
routes like post/new
*/
var post = require('./routes/post');
app.use('/post',post)
This works fine however i want to include this into another file lets say config/route-main.js
and link this to app.js
file 效果很好,但是我想将其包含到另一个文件中,比如说
config/route-main.js
并将其链接到app.js
文件
How can i do that?? 我怎样才能做到这一点??
in app.js
include 在
app.js
包括
var routes = require('./config/route-main');
app.use('/',routes);
and in route-config.js 并在route-config.js中
var express = require('express');
var router = express.Router();
var comment = require('./routes/comment');
var post = require('./routes/post');
router.use('/comment',comment);
router.use('/post',post)
module.exports = router
check this link http://katieleonard.ca/blog/2016/nested-routes-with-expressjs/ 检查此链接http://katieleonard.ca/blog/2016/nested-routes-with-expressjs/
Your app.js
file: 您的
app.js
文件:
const Express = require('express');
const app = Express();
const Routes = require('./routes');
app.use('', Routes);
Your routes.js
file: 您的
routes.js
文件:
const Express = require('express');
const Router = Express.Router();
const UsersController = require('./controllers/usersController');
Router.post('/users', UsersController.CreateUser);
module.exports = Router;
Your usersController.js
file: 您的
usersController.js
文件:
const CreateUser = (req, res, next) => {
// DO STUFF HERE
}
module.exports = {
CreateUser
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.