Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] Api routes domain-dependently #53

Open
ryuina opened this issue Aug 5, 2023 · 2 comments
Open

[BE] Api routes domain-dependently #53

ryuina opened this issue Aug 5, 2023 · 2 comments
Assignees

Comments

@ryuina
Copy link
Collaborator

ryuina commented Aug 5, 2023

Set api routes domain dependently

app.use("/api", authRoutes);
app.use("/api", categoryRoutes);
app.use("/api", ageCategoryRoutes);
app.use("/api", productRoutes);
app.use("/api", blogCategoryRoutes);
app.use("/api", blogPostRoutes);
app.use("/auth", authGoogle);

TO-BE

(example)

app.use('/api/auth', authApiRoutes)
app.use('/api/blogs', blogApiRoutes)
app.use('/api/products', productsApiRouters)

then you don't need to text 'blogs' or 'products' on every api routes

Comments

  • "domain" is really important words on Back-end
  • By dividing their "zones" by using more specified prefixes, you can prevent "route intersect error"
    • sometimes we can make error if different api routes are same unfortunately but cannot find it because they are written on different files
    • So 'Zoning' apis are really important
@ryuina ryuina self-assigned this Aug 5, 2023
YoungSong99 added a commit that referenced this issue Aug 11, 2023
update blog, blog category api routes
YoungSong99 added a commit that referenced this issue Aug 11, 2023
update: Api routes domain-dependently #53
@YoungSong99
Copy link
Contributor

Thank you for your suggestion!
I've successfully updated the blog API, and I have a few questions before making changes to other routes.

There are instances where the same keyword isn't consistent within a route. For instance, in the server/routes/category.js file, the route for listing categories is /categories, not /category/list. Should I consider changing /categories to /category/list for consistency?

router.post("/category", requireSignin, isAdmin, create);
router.put("/category/:categoryId", requireSignin, isAdmin, update);
router.delete("/category/:categoryId", requireSignin, isAdmin, remove);
router.get("/categories", list);
router.get("/category/:slug", read);

Regarding the authentication routes (server/routes/auth.js), updating might be a bit challenging due to the variety of keywords used.

router.post("/register", register);
router.post("/login", login);
router.put("/profile", requireSignin, updateProfile);
router.get("/auth-check", requireSignin, (req, res) => {
    res.json({ ok: true });
});
router.get("/admin-check", requireSignin, isAdmin, (req, res) => {
    res.json({ ok: true });
});
router.get("/orders", requireSignin, getOrders);
router.get("/all-orders", requireSignin, isAdmin, allOrders);
router.get("/secret", requireSignin, isAdmin, secret);

Furthermore, certain paths (such as product, category, and blog) might be organized, while others aren't.

app.use("/api", authRoutes); 
app.use("/api/category", categoryRoutes);
app.use("/api/ageCategory", ageCategoryRoutes);
app.use("/api/product", productRoutes);
app.use("/api/blog", blogCategoryRoutes);
app.use("/api/blog", blogPostRoutes);
app.use("/auth", authGoogle);
app.use("/index", indexGoogle);

Does this lack of consistency affect the overall cohesiveness of the code?

@ryuina
Copy link
Collaborator Author

ryuina commented Aug 11, 2023

@HyangYoung
If I do refactoring on this, I'll follow REST rule.
With REST API rule, the api starts from the domain name.
So if a table for category is named "category" then the apis starts from /api/category and the list get will be / inside the route file.
Or else, the table's name is "categories" then the api will starts with the prefix /api/categories and the follows are / /[id] and so on.
but sometimes, by depends on one's preference, /api/category/categories /api/category/[id] is available too.
Anyway everything is not "must" or even not "should". It's for developers' experience.
"Dividing api domains" is for DX, and the REST rule also for DX. So if all over the developers who have to read or use this agree, then it's okay!

On the second one, I'll grouping them as /api/auth :) And I think the apis about "order" should be divided if they are about the orders.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants