Home > other >  move express js app.use () to another file
move express js app.use () to another file

Time:10-31

I have many routers in my routes folder. when I want to use them in my main server file, it takes too many lines. Can i move these to another file? if yes how?

ex:

    
app.use("/user", userRouter);
app.use("/user", loginRouter);
app.use("/role", roleRouter);
app.use("/rolePermission", rolePermissionRouter);
app.use("/module", moduleRouter);
app.use("/menu", menuRouter);
app.use("/menuItem", menuItemRouter);
app.use("/news", newsRouter);
app.use("/newsItem", newsItemsRouter);
app.use("/news/category", newsCategoryRouter);
app.use("/news/comments", newsCommentRouter);
app.use("/news/comments/user", newsCommentUserRouter);
app.use("/setting", settingRouter);
app.use("/news/rating/user", newsRatingUserRouter);
app.use("/news/tag/user", newsTagUserRouter);
app.use("/linkbox", linkboxRouter);
app.use("/linkboxItem", linkboxItemRouter);
app.use("/linkbox/category", linkboxCategoryRouter);
app.use("/faq", FAQRouter);
app.use("/faq/category", FAQCategoryRouter);
app.use("/faqItem", FAQItemRouter);
app.use("/contact", contactRouter);
app.use("/contactItem", contactItemRouter);
app.use("/imageGallery", imageGalleryRouter);
app.use("/imageGallery/category", imageGalleryCategoryRouter);
app.use("/imageGallery/comment", imageGalleryCommentRouter);
app.use("/imageGallery/comment/user", imageGalleryCommentUserRouter);
app.use("/imageGalleryItem", imageGalleryItemRouter);
app.use("/imageGallery/tag/user", imageGalleryTagUserRouter);
app.use("/imageGallery/rating/user", imageGalleryRatingUserRouter);

CodePudding user response:

There are multiple ways to implement this, but I would recommend to create a new router in between, and register all routes there. Like you already did with e.g the userRouter.

You can create a new file router.js with the following content:

import { Router } from 'express';

const router = Router();
router.use("/user", userRouter);
router.use("/user", loginRouter);
router.use("/role", roleRouter);
router.use("/rolePermission", rolePermissionRouter);
router.use("/module", moduleRouter);
router.use("/menu", menuRouter);
router.use("/menuItem", menuItemRouter);
router.use("/news", newsRouter);
router.use("/newsItem", newsItemsRouter);
router.use("/news/category", newsCategoryRouter);
router.use("/news/comments", newsCommentRouter);
router.use("/news/comments/user", newsCommentUserRouter);
router.use("/setting", settingRouter);
router.use("/news/rating/user", newsRatingUserRouter);
router.use("/news/tag/user", newsTagUserRouter);
router.use("/linkbox", linkboxRouter);
router.use("/linkboxItem", linkboxItemRouter);
router.use("/linkbox/category", linkboxCategoryRouter);
router.use("/faq", FAQRouter);
router.use("/faq/category", FAQCategoryRouter);
router.use("/faqItem", FAQItemRouter);
router.use("/contact", contactRouter);
router.use("/contactItem", contactItemRouter);
router.use("/imageGallery", imageGalleryRouter);
router.use("/imageGallery/category", imageGalleryCategoryRouter);
router.use("/imageGallery/comment", imageGalleryCommentRouter);
router.use("/imageGallery/comment/user", imageGalleryCommentUserRouter);
router.use("/imageGalleryItem", imageGalleryItemRouter);
router.use("/imageGallery/tag/user", imageGalleryTagUserRouter);
router.use("/imageGallery/rating/user", imageGalleryRatingUserRouter);
export default router;

And than import this router into the previous file (like e.g. server.js or main.js), that you are using to configure the express app object, and replace all our previous 30 route registrations with the new router like:

import GlobalRouter from './router.js';

[...]

app.use(GlobalRouter);

[...]
  • Related