11-Nodejs Course 2023: Users Module
Tags: node, typescript, mongodb, fastify
Published: 2022-10-29 19:03:24

---
title: 11-Nodejs Course 2023: Users Module
published: true
description: 
series: Nodejs Course 2023
tags: nodejs, typescript, mongodb, fastify
#cover_image: 
# Use a ratio of 100:42 for best results.
---

Our previous article was about the application structure, and we talked about the modules, and how we're going to structure our modules, now let's create a new module, which is the `users` module.

## Users Module

Basically, the module will consist of only one request, list all users, so we need to define two things, the request route (path) and the request handler (controller).

## Users Route

Now let's create a new file called `routes.ts` inside the `users` module, and let's define the route for our request.

```ts
// src/app/modules/users/routes.ts
import router from "./../../core/router";

router.get("/users", (req, res) => {
  res.send("Hello Users");
});
```

We imported the router from the `core` module, and we used it to define a new route, which is a `GET` request to the `/users` path, and we defined a request handler, which is a function that will be called when the request is received, and it will receive two parameters, the request and the response.

But the import here is so ugly, Let's add an alias for our most used folders that we're going to use, this is where we will head to `tsconfig.json` file.

## Path Aliases

We need to add a new property to the `compilerOptions` object, which is `paths`, and we need to define the aliases we want to use, and the paths they will point to.

```json
{
  "compilerOptions": {
    "paths": {
      "core/*": ["src/core/*"],
      "app/*": ["src/app/*"]
    }
  }
}
```

We added here two aliases, `core` and `app`, and we defined the paths they will point to.

Now let's use the new aliases in our code.

```ts
// src/app/modules/users/routes.ts
import router from "core/router";

router.get("/users", (req, res) => {
  res.send("Hello Users");
});
```

Pretty elegant, right?

## Users Controller

Now let's create a new file called `user.controller.ts` inside the `controllers` directory, and let's define the controller for our request.

```ts
// src/app/modules/users/controllers/list-users.controller.ts

export default function listUsers(request: any, response: any) {
  res.send("Hello Users");
}
```

We exported a function called `listUsers`, and it receives two parameters, the request and the response, and it will send a response with the text `Hello Users`.

Now let's use the controller in our route.

```ts
// src/app/modules/users/routes.ts
import router from "core/router";
import listUsers from "./controllers/list-users.controller";

router.get("/users", listUsers);
```

And that's it, now let's create a new file called `routes.ts` in our src directory, and let's import the `users` module routes.

```ts
// src/routes.ts
// users routes
import "app/modules/users/routes";
```

Our final step is it import the `routes.ts` file in our `index.ts` file.

```ts
// src/index.ts
import startApplication from "./core/application";
import "./routes";

startApplication();
```

And that's it!

Now let's try it.

Head to your terminal, and run the following command.

```bash
yarn start
```

And now head to `http://localhost:3000/users`, and you should see the text `Hello Users`.

## 🎨 Project Repository

You can find the latest updates of this project on [Github](https://github.com/hassanzohdy/nodejs-2023)

## 😍 Join our community

Join our community on [Discord](https://discord.gg/vtZE9YzaFz) to get help and support (Node Js 2023 Channel).

## 🎞️ Video Course (Arabic Voice)

If you want to learn this course in video format, you can find it on [Youtube](https://www.youtube.com/playlist?list=PLGO8ntvxgiZMJc7RN2lIq9WmMOlWZGzmz), the course is in Arabic language.

## 💰 Bonus Content 💰

You may have a look at these articles, it will definitely boost your knowledge and productivity.

General Topics

- [Event Driven Architecture: A Practical Guide in Javascript](https://dev.to/hassanzohdy/event-driven-architecture-the-best-paradigm-that-i-love-to-work-with-in-javascript-and-node-js-1gnk)
- [Best Practices For Case Styles: Camel, Pascal, Snake, and Kebab Case In Node And Javascript](https://dev.to/hassanzohdy/best-practices-for-case-styles-camel-pascal-snake-and-kebab-case-in-node-and-javascript-55oi)
- [After 6 years of practicing MongoDB, Here are my thoughts on MongoDB vs MySQL
](https://dev.to/hassanzohdy/after-6-years-of-practicing-mongodb-here-are-my-thoughts-on-mongodb-vs-mysql-574b)
-

Packages & Libraries

- [Collections: Your ultimate Javascript Arrays Manager](https://dev.to/hassanzohdy/collections-your-ultimate-javascript-array-handler-3o15)
- [Supportive Is: an elegant utility to check types of values in JavaScript](https://dev.to/hassanzohdy/supportive-is-an-elegant-utility-to-check-types-of-values-in-javascript-1b3e)
- [Localization: An agnostic i18n package to manage localization in your project](https://dev.to/hassanzohdy/mongez-localization-the-simplest-way-to-translate-your-website-regardless-your-favorite-framework-4gi3)

 React Js Packages

- [useFetcher: easiest way to fetch data in React Js](https://dev.to/hassanzohdy/usefetcher-the-easiest-way-to-fetch-data-in-react-45o9)

Courses (Articles)

- [React Js: Let's Create File Manager With React Js and Node Js](https://dev.to/hassanzohdy/lets-create-a-file-manager-from-scratch-with-react-and-typescript-chapter-i-a-good-way-to-expand-your-experience-5g4k)