Assuming there's a mongoose model defined for the object you want to serve, first install the required packages:
index.ts36 chars2 lines
Then import the required packages and connect to the mongoose database:
index.ts236 chars9 lines
Next, create an express route to handle the request and lookup the object in the database:
index.ts622 chars24 lines
Finally, mount the router in your express app:
index.ts189 chars9 lines
This will create an express app with a router that handles GET requests to /api/myroute/:id
. When a request is received, the corresponding object is looked up in the database by id and served as a JSON response.
gistlibby LogSnag