DEV Community

Hasan Zohdy
Hasan Zohdy

Posted on

9-Nodejs Course 2023: Application Encapsulation

Now everything looks pretty good in our router file, but the index file is a total mess, so let's encapsulate it.


Create a new file in src/core and name it application.ts

// src/core/application.ts

import Fastify from "fastify";
import router from "./router";

export default async function startApplication() {
  const server = Fastify();


  try {
    // 👇🏻 We can use the url of the server
    const address = await server.listen({ port: 3000 });

    console.log(`Start browsing using ${address}`);
  } catch (err) {
    process.exit(1); // stop the process, exit with error
Enter fullscreen mode Exit fullscreen mode

All what we did here is we moved the code from the index file into the application file, the code is now more easier to maintain specially when the application goes bigger.

Now we can use startApplication function in our index.ts file.

// src/index.ts

import startApplication from "./core/application";

Enter fullscreen mode Exit fullscreen mode

In our next lesson we will start working on the application logic itself and how we can map our routes with its own handlers (Controllers).

🎨 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

Packages & Libraries

React Js Packages

Courses (Articles)

Top comments (0)