Skip to content

Setup

alexfinger21

About 218 wordsLess than 1 minute

2025-07-31

Intro

Routers serve as a way to fragment the server code into page-specific JS files. Routers are imported in server/server.js like so: Let's take a look at the admin-page router, which is concise but still incorporates all router functions:

Adjustments

Although there is definitely some codebase maintenance to be done (the database queries should be placed under database/database.js and then imported), the basic elements are still there:

  • Create an express router - express.Router()
  • The router then functions exactly the same as any express URL endpoint handler
  • For each request method (e.g. GET/POST), define a handler function
  • Return value using res (can be anything like rendering EJS using res.render or just returning json using res.send methods)
  • Finally, export the router using the CommonJS module.exports convention (module.exports = router)

Note: This project currently uses the CommonJS module system for exporting routers (module.exports = router). If you are using ES6 modules, you can export the router with export default router;. Please ensure consistency with the rest of the codebase and use CommonJS unless otherwise specified. It's that simple!