11-Nodejs Course 2023: Users Module
Hasan Zohdy
Posted on October 29, 2022
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.
// 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.
{
"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.
// 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.
// 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.
// 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.
// 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.
// 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.
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
😍 Join our community
Join our community on Discord 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, 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
- Best Practices For Case Styles: Camel, Pascal, Snake, and Kebab Case In Node And Javascript
- After 6 years of practicing MongoDB, Here are my thoughts on MongoDB vs MySQL -
Packages & Libraries
- Collections: Your ultimate Javascript Arrays Manager
- Supportive Is: an elegant utility to check types of values in JavaScript
- Localization: An agnostic i18n package to manage localization in your project
React Js Packages
Courses (Articles)
Posted on October 29, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.