好吧,我的routes文件夹中有routes.js文件
const express = require("express");
const router = express.Router();
const passport = require("passport");
const User = require("../modules/users/models/users");
var cookieParser = require('cookie-parser')
var csrf = require('csurf')
var bodyParser = require('body-parser')
// Controllers
const authController = require('../modules/users/controllers/auth');
const socialAuthController = require('../modules/users/controllers/socialAuth');
const email = require('../modules/users/controllers/email');
const profile = require('../modules/users/controllers/profile');
// CSRF
var csrfProtection = csrf({
cookie: true
})
var parseForm = bodyParser.urlencoded({
extended: false
})
// Register
router.get("/register", csrfProtection, shouldNotBeAuthenticated, function (req, res) {
res.render("../modules/users/views/register", {
title: 'Register',
csrfToken: req.csrfToken
});
});
router.post("/register", parseForm, csrfProtection, authController.user_reigster);
// Login
router.get("/login", shouldNotBeAuthenticated, function (req, res) {
res.render("../modules/users/views/login", {
title: 'Login'
});
});
router.post("/login", authController.user_login);
// Twitter AUTH
router.get("/twitter/login", passport.authenticate("twitter"));
router.get("/twitter/return", socialAuthController.auth_twitter);
// social, users, articles AND MORE...
module.exports = router;
但是后来我有了文件夹模块
>USERS
----------->CONTROLLERS
----------->MODELS
----------->VIEWS
>ARTICLES
----------->CONTROLLERS
----------->MODELS
----------->VIEWS
我想做的是将路线文件夹添加到用户,文章...,这样我就可以拥有完整的模块。 但是我不知道如何使它变得聪明。例如,我想将所有模块的路由自动导入主route.js或以某种方式巧妙地导入,因为我的代码开始看起来很乱。
答案 0 :(得分:1)
您可以将路由分成单独的文件,如下所示:
users / routes.js:
const express = require("express");
const router = express.Router();
const passport = require("passport");
const User = require("../modules/users/models/users");
var cookieParser = require('cookie-parser')
var csrf = require('csurf')
var bodyParser = require('body-parser')
// Controllers
const authController = require('../modules/users/controllers/auth');
const socialAuthController = require('../modules/users/controllers/socialAuth');
const email = require('../modules/users/controllers/email');
const profile = require('../modules/users/controllers/profile');
// CSRF
var csrfProtection = csrf({
cookie: true
})
var parseForm = bodyParser.urlencoded({
extended: false
})
// Register
router.get("/register", csrfProtection, shouldNotBeAuthenticated, function
(req, res) {
res.render("../modules/users/views/register", {
title: 'Register',
csrfToken: req.csrfToken
});
});
router.post("/register", parseForm, csrfProtection, authController.user_reigster);
// Login
router.get("/login", shouldNotBeAuthenticated, function (req, res) {
res.render("../modules/users/views/login", {
title: 'Login'
});
});
router.post("/login", authController.user_login);
// Twitter AUTH
router.get("/twitter/login", passport.authenticate("twitter"));
router.get("/twitter/return", socialAuthController.auth_twitter);
module.exports = router;
您的文章路线也是如此:
articles / routes.js:
const express = require("express");
const router = express.Router();
// Some routes here...
module.exports = router;
然后在主应用程序文件中,只需导入并声明所有单独的路由器文件:
app.js
const express = require("express");
const app = express();
const router = express.Router();
const userRoutes = require('./users/routes'); // assuming the path is right ...
const articleRoutes = require('./articles/routes');
router.use(userRoutes);
router.use(articleRoutes);
app.use('/', router);