Tuesday 25 September 2018

Getting confused designing REST API that has Users

I have an auth router and an api router for my Koa app.The auth router looks something like:const router = new Router({ prefix: '/auth' }) router.post('/signup', async ctx => { ... }) router.post('/login', authenticate(), async ctx => { ... }) router.post('/logout', async ctx => { ... }) and the api router looks something like:const router = new Router({ prefix: '/api' }) // Get user by ID router.get('/users/:userId', async ctx => { ... }) // Create a user router.post('/users', async ctx => { ... }) // Delete a user router.delete('/users/:userId', async ctx => { ... }) // Get food by ID router.get('/foods/:foodId', async ctx => { ... }) ... The main question I have is about the users part of it (not the food stuff). I am noticing that both the POST /users and the POST /signup do the same thing (essentially call myDB.createUser()). I am wondering if I shouldn't have a POST /users in my API at all. I just felt weird not having it because the food stuff and other API routes are probably going to have POST, GET, DELETE, and PUT methods in each of them.TLDR: Should I only have 1 create user route (instead of 2), or should I somehow be calling POST /users in my POST /signup route.I do have one other small question – should I be desigining my API to be like GET /users/:userId or GET /users:/username or somehow allow both?

Submitted September 25, 2018 at 11:49PM by intftbbts

No comments:

Post a Comment