In todays's tutorial, we are going to implement Full Calendar with Node.js & MongoDB. To implement API, we use light weight Node.js web application framework Express and we use MongoDB to store our Events data.
Prerequisites
Node.js
To Continue with this tutorial, you should have installed Node.js in your pc. If you are still not installed Node.js in your machine you can download it from here.
Mongo DB
To Continue with this tutorial, you should have installed Mongo DB in your pc. If you are still not installed Mongo in your machine you can download it from here.
Postman
To test our API endpoints, you need a API Testing application like Postman. You can download Postman from here.
Setting up the Project
First you need to create a directory for our project. If you are going to use terminal to create directory, run below command on your termianl.
1
mkdir angulr-fullcalendar
Then navigate to your project directory by using below command in your terminal
1
cd angulr-fullcalendar
Then we need to create directory for our server application. After creating that, you need to navigate to that server directory where our API going to implement. Now we need to initialize a new package.json:
1
npm init
Next, we need to install to required dependencies:
1
npm install --save express body-parser express mongoose nodemon cors
At this point, we have been initialized our project and all the dependencies have been installed. Usually we start a project with creating our entry file such like server.js. Create a file called server.js in your root directory and then add below code in it:
mongoose.connect(CONNECTION_URL, { useNewUrlParser: true, useUnifiedTopology: true }) .then(() => app.listen(PORT, () => console.log(`Server Running on Port: http://localhost:${PORT}`))) .catch((error) => console.log(`${error} did not connect`));
mongoose.set('useFindAndModify', false);
We have created our server.js file but we still missing our routes paths which will be added at the end. Now, we can run our server.js file by running npm strat command in your server directory terminal.
Note: You need to change your `package.json` file
1 2 3
"scripts": { "start": "nodemon server.js" },
When you run above npm start command, the server will start and it will show Server Running on Port: http://localhost:5000 in your terminal. Now, we need to proceed with our Database section.
Setting up the Database
Create a directory call models in your project's root directory. Then, inside that model directory create a file called Event.js where our event schema going to be stored and place below code:
Now, we need to create our controllers which need to fetch our all events, create new event and delete event. So, before moving to coding part you need to create a directory call controllers in your project's root directory. Then, inside that controllers directory create a file called event.js and place below code:
We have already finished with our Models and Controllers. So, now we need to map our funtion in our Controller with the routes. To do it, create a directory call routes in your project's root directory. Then, inside that routes directory create a file called events.js and place below code:
1 2 3 4 5 6 7 8 9 10 11
import express from 'express';
import { getEvents, createEvent, deleteEvent } from '../controllers/events.js';
To create our client side application run below command in your `angular-fullcalendar` root directory:
1
ng new client
This command will create a fresh angular project for us.
Install dependencies
We need two additional dependencies for this project. You can install them by running below command in your client folder's terminal:
1
npm i --save @fullcalendar/angular @fullcalendar/daygrid @fullcalendar/interaction @sweetalert2
Create API service
Now, we need to create `api service` file, which holds our base url path of our server. Create a folder called services in our app and then run below command in your terminal:
We need to create two new components in our Angular application. Create a directory called pages in app directory and then run below command in terminal:
1 2 3
ng new c pages/calendar
ng new c pages/calendar/add-event
This command will create two new componets which hold our calendar and add event form. Now open calendar.component.ts and paste below command:
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { HttpClientModule } from '@angular/common/http';
import { AppRoutingModule } from './app-routing.module'; import { FormsModule } from '@angular/forms'; import { AppComponent } from './app.component'; import { CalendarComponent } from './pages/calendar/calendar.component'; import { FullCalendarModule } from '@fullcalendar/angular'; // the main connector. must go first import dayGridPlugin from '@fullcalendar/daygrid'; // a plugin import interactionPlugin from '@fullcalendar/interaction'; import { AddEventComponent } from './pages/calendar/add-event/add-event.component';
providers: [], bootstrap: [AppComponent] }) export class AppModule { }
Now, you can check our application by running ng serve in your client firectory and npm start in server directory.
Conclusion
In this tutorial, we implemented Angular Full Claendar example with Node.js API and MongoDB. If you have any issue regarding this tutorial, mention your issue in comment section or reach me through my E-mail. You can obtain complete source code for this tutorial from this GitHub repository.