I've built a service using Node, Express & Mongoose. At the moment it renders templates to the front-end but I want to extend the functionality to a full API.
I'd like to use a single route for each type of resource to be returned with a flag to tell the server to return API content or to render the view and respond with the HTML.
Examples of this would be as follows:
/users/:userId/api/users/:userId
Both would use the same function (i.e. users.getOne) which would then check if the path api is present and send the correct response accordingly.
I could obviously just do the following:
app.get( '/users/:userId', users.getOne );
app.get( '/api/users/:userId', users.getOne );
and check in the response handler, though I'd rather not have 2 routes defined for each resource.
Aucun commentaire:
Enregistrer un commentaire