gpt4 book ai didi

javascript - 表达 js :multiple route files into single file

转载 作者:行者123 更新时间:2023-11-29 16:40:07 25 4
gpt4 key购买 nike

在我的 express.js 应用程序中,我按以下方式组织我的路线

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(处理所有post路由)

var express = require('express');
var router = express.Router();
var post = require('../controller/post');
router.route('/new').get(post.create);
module.exports = router;

我像这样在 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)

这很好用,但是我想将它包含到另一个文件中,比如 config/route-main.js 并将其链接到 app.js 文件

我该怎么做?

最佳答案

app.js 中包含

var routes  = require('./config/route-main');
app.use('/',routes);

在 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

检查此链接:

https://katieleonard.ca/blog/2016/2016-04-12-nested-routes-with-expressjs/

关于javascript - 表达 js :multiple route files into single file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47125420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com