Creating server side apis for attendance system

expressdefault

Hey guys in the last post we designed the frontend of attendance marking system. In todays post we are going to design the backend of the system using node(express) and mongodb.
You can find complete codebase of this project on github
If you have not installed mongodb yet here is the tutorial.
The steps are as follows:
1)Create a new directory for backend and install the following modules:
npm install --save express express mongoose moment body-parser
Start your mongodb service
service mongod start

2) Lets create basic express server using routes:

Here body-parser is used to parse json data in request from client.
We are using express router to add various routes that are going to be handled by the server.
Here all routes are handled with prefix api/ because we have used api at line 14.

It would be better to use nodemon rather than node command to run server since it restarts the server on changes in files, kind of like live-reload on server side.
To install nodemon run the following command:
npm install -g nodemon
Then run nodemon server.js to start server.
You can see response message on localhost:3000/api as follows:
expressdefault

If you come across cross origin request blocked in console of your browser then add the follwoin code:

This simply acts as a middleware which allows cross origin requests to server.
3)We are going to use mongoose module to connect to mongodb.
So include following code in the beginning of server.js to include mongoose module.

Here attendancedb is our default database or collection.

4)Lets create a schema for documents by creating models folder in present directory and create attendance.js in the models folder.
The schema for attendance document is as follows:

As you can see the schema is similar to json for records developed in react in last tutorial.
We use required as true so that the attributes are validated to be present before addition to mongodb collection.
5)Now lets create handler functions for various routes.it is a better idea to write handlers in different file.So create routes directory in parent folder and create attendanceroutes.js within it.

First we include attendance schema in this file as well as moment which will be required further:

Next we create handler to mark attendance i.e create documents in mongodb collection.

Here a document is created using post data from request on this handler.

Next we create a handler to get all records stored in attendancedb collection.

The find function is used to retrieve all documents from collection.
Here we are formatting the response before sending to filter out the id and other unnecessary mongodb attributes.

Next we create handler to get data for specific month:

Here we get month as integer value(ex value as 1 for January) and year in as payload in post request.
Next we get starting and ending date for that month using moment.We need to subtract 1 since in moment month value starts from 0.
Next we add condition in find query to retrieve all records whose start value is greater than starting date of month and end value is less than ending date of the month.
We again filter the results and send back the data in reply.

Next we write a handler to get data in a specific date range:

6)Next we create appropriate routes for each handler function:

Here getallrecords is a get request while all others all post requests.
We can check the post apis using postman app in chrome.
Make sure that you select method as post and body type as application/json.
attendaceapis
Here are payloads for each apis to test:
Api:http://localhost:3000/api/markattendance
Payload:
{
“title”: “Present”,
“allDay”: false,
“start”: “Tue Sep 13 2016 11:13:00”,
“end”:”Tue Sep 13 2016 11:13:00″
}

Api:http://localhost:3000/api/getbymonth
Payload:
{
“month”: 9,
“year”:2016
}

Thats it for todays post. In the next tutorial we will see how to connect the frontend to backend using ajax requests. Till then try out new frameworks and happy coding.

Share post-
About Saurabh Mhatre 70 Articles
Currently working in web and hybrid application development