A simple RESTful dockerized Node + MongoDB app for adding and retrieving books.
If you have Docker installed in your computer, you do not need to have Node.js or MongoDB installed. You can run the application with a single command:
$ docker-compose up
Without Docker, you need to install Node.js, NPM and MongoDB.
To install Node.js and NPM, refer to the documentation provided here.
To install MongoDB, refer to the documentation provided here.
To start MongoDB:
- Windows:
$ "C:\Program Files\MongoDB\Server\4.4\bin\mongo.exe"
- Mac:
$ mongod --config /usr/local/etc/mongod.conf
- Ubuntu: Please refer to the documentation provided at the link.
NOTE: Database and collection is automatically created with the first POST
request to /api/books
. No MongoDB configuration is required after installation - keeping MongoDB running is sufficient.
To get the Node server running locally:
npm install
to install all required dependencies from package.json:$ npm install
npm run start
to start the local server:$ npm run start
npm run dev
to start the local server in development mode:$ npm run dev
- expressjs - The server for handling and routing HTTP requests.
- mongoose - MongoDB object modeling tool designed to work in an asynchronous environment.
- morgan - HTTP request logger middleware for node.js.
- nodemon - The development server that automatically restarts the node application when file changes in the directory are detected.
index.js
- The entry point to our application. This file defines the express server, integrates all the routes, initializes the logger and the mongoDB connection.src/controller/
- This folder contains the controllers for the app to read and write data to MongoDB.src/routes/
- This folder contains the route definitions for the app.src/models/
- This folder contains the Mongoose Schema definition for the 'books' resource.test/
- This folder contains the POSTMAN collection with all requests configured.
Returns json containing newly added data.
-
URL
/api/books
-
Method:
POST
-
URL Params
None
-
Query Params
None
-
Data Params
Required:
title: [string]
author: [string]
-
Success Response:
- Code: 201 CREATED
Content:{ "id": "abcdefg1234, "title": "Dockerizing your personal projects", "author": "Dhanush Kamath" }
- Code: 201 CREATED
Returns json data containing details of all books.
-
URL
/api/books
-
Method:
GET
-
URL Params
None
-
Query Params
None
-
Data Params
None
-
Success Response:
- Code: 200
Content:{ "books": [ { "title": "Dockerize your personal projects!", "author": "Dhanush Kamath", "createdAt": "2020-12-24T20:59:42.516Z" }, { "title": "Burgers and Cars: An Autobiography", "author": "Dhanush Kamath", "createdAt": "2020-12-24T21:04:27.111Z" } ] }
- Code: 200
- Dhanush Kamath - dhanushkamath