# Outline

## **Initiating Server**

```
mongod
```

## **Mongod**

is the primary daemon process for the MongoDB system.

```
npm start
```

Starts the node server and executes`index.js`of the project.

## **Index.js**

Mainly performs three tasks in following order:

1. **MongoDB connection:** Mongoose makes a connection to mongoDB.
2. **Socket connection:** Starts the socket server (/config/socket.js)
3. **Node server:** Starts the node server and configure port (/config/express.js)

## **MongoDB Models**

All the mongoDB models or schemas are defined under `/server/models.`

## **Socket Server**

Socket-server performs following functionalities :

1. Starts the socket server
2. Once when client is connected, it checks for authentication
3. If successfully authenticated, Socket Handler is called to handle different socket scenarios (/server/socketHanlder/index.js)

## **Socket Handler**

* **messageHandler :** Handles all the chat events between users (message-handler.js)

## **Express JS**

Express JS contains all the configuration details related to node server. It performs the following tasks:

1. Create and start server
2. Add different middleware namely:
3. * **bodyParser:** Parse body params and attache them to req.body
   * **cookieParser:** Parse Cookie header and populate req.cookies with an object added as key by the cookie names.
   * **Compress:** Helps in decreasing the size of the response body and improve the speed of the API calls.
   * **Passport:** Helps to authenticate different API call.
   * **Helmet:** Helps to protect the application from different attacks such as cross-site scripting (XSS), script injection.
   * **Cors:** It helps in connecting the different domain to the application i.e enable cross-origin resource sharing.
   * **APIError:** Handles any other error that occurred inside of an API calls such as API not found, internal server error, parameter validation error.
   * **API route:** Manage various other routes of the application (/server/routes/index.js)

## **Passport JS**

This module lets you authenticate endpoints using a JSON web token and passport-jwt. A passport middleware is added to all the routes and socket which will check for authorized token. We can also change the strategy of the passport in the passport-config.jsfile.

## **Api Routes**

It handles the different routes of the application, namely:

* **users:** This route contains different routes related to the user such as registration of the new user, fetching user details, etc.
* **auth:** Manages the login, checkUser and logout route. This route handles the authentication of the user by generating the token.
* **apartment:** Handles the user apartment related route such as getting the apartments of the user, updating apartment details etc.
* **messages:** handles the chat related routes such as userMessages, loadMore messages etc.

## **Controllers**

Following are the controllers defined for the above-mentioned routes:

* **User Controller** Performs user related functions, such as:
* * Create new user
  * Get user details
  * Update user-details
  * GetfavouriteUsers
* **Auth Controller** Performs user authentication tasks: Login ,LogOut, checkUser
* **Apartment Controller** Defines apartment related functions such as  getApartments, update apartment details etc.
* **Messages Controller:** Defines users chat related functions such as fetch messages, loadMoreMessages, fetch specific user messages etc.
