This is a demo project created while following the Build A Restful Api With Node.js Express & MongoDB.
I did this tutorial as a followup after dotnet-core-mvc-rest-api to compare two technologies.
express
- web servernodemon
- watches for source changes & restarts node appmongoose
- client for MongoDBbody-parser
- parses request JSON body into object
Messages in Express are processed via a simple pipeline consisting of
middleware
= functions which are called first and which can modify requestroute handlers
= functions registered to be called for a specific route and request type
Middleware is registered via add
method of the Express.
In this example we use two middlewares:
bodyParser
- converts requst body (JSON) into JavaScript objectsrouter
- calls route handlers
Route handler functions are registered via a method named after the corresponds HTTP request type:
post
= POST request handlerget
= GET request handlerdelete
= DELETE request handler- etc...
While registering a handler we specify the URL to which the handler will respond. The URL string can contain parameter placeholders which will get passed to the requst handler function.
Response is sent to client via resp
object which is passed to the handler function as an argument.
Here's an example showing how to respond with HTTP status 200 and a JSON response type:
resp.status(200).json(savedPost);
Mongoos npm package is used to access the MongoDB database.
Querying is done via a model object of type Schema
.
In model we describe the structure of the record stored in the DB, while the Schema
methods we can use to query the database. Here are a few examples:
save
- adds record to the databasedeleteOne
- deletes one record from the databasefind
- finds a record in the DB