When you wish to consolidate all CRUD operations for your model into one file, it is typically done using the following structure as shown in our first method:
routes.js
const express = require("express");
const users = require("../routes/users");
module.exports = function (app) {
app.use(express.json());
app.use("/api/user", users);
};
users.js
router.patch("/", auth, async (req, res) => {
// create and update operations ...
})
router.delete("/:id", auth, async (req, res) => {
// delete operations ...
})
module.exports = router
However, if you prefer having separate files for each CRUD operation, you can achieve this using a different approach known as the second method. Here's how the directory structure would look like:
users
|__patchUser.js
|__deleteUser.js
|__index.js
The index file should resemble the following:
index.js
const express = require("express");
const router = express.Router();
module.exports = router;
Furthermore, the other individual files will be structured as follows:
patchUser.js
const router = require("./index");
router.patch("/", auth, async (req, res) => {
})
Unfortunately, this setup does not work as expected.
How can we correct the second method to have distinct CRUD routing files?