75-Nodejs Course 2023: Break IV: Urls Functions
Tags: node, typescript, mongodb, fastify
Published: 2022-11-29 17:35:39

---
title: 75-Nodejs Course 2023: Break IV: Urls Functions
published: true
description: 
series: Nodejs Course 2023
tags: nodejs, typescript, mongodb, fastify
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uj788v79vqk0qerqdrkr.png
# Use a ratio of 100:42 for best results.
---

We were done with `paths` functions now let's move to another `utility` file, the `urls`

## Why Urls

The purpose of this file that we are going to create is just to generate the urls for our project, so we can use it in our project easily.

## Urls functions list

Now let's list our functions that we're going to use in our project.

* `url`: Receives a relative path and returns the absolute url to it.
* `uploadsUrl`: Receives a relative path to the uploads folder and returns the absolute url to it.
* `publicUrl`: Receives a relative path to the public folder and returns the absolute url to it.

Now let's move to the implementation.

## Implementation

Now create `urls.ts` file inside `src/utils` folder, and add the following code:

```ts
// src/utils/urls.ts

let baseUrl = ''

/**
 * Get full path for the given path
 */
export function url(path = "") {
  return baseUrl + path;
}

/**
 * Get uploads url
 */
export function uploadsUrl(path = "") {
  return url("/uploads/" + path);
}

/**
 * Get full path for the given path related to public route
 */
export function publicUrl(path = "") {
  return url("/public/" + path);
}
```

So we just added a `baseUrl` variable that will hold the base url that were coming from the `Fastify` instance, and we added three functions, `url`, `uploadsUrl` and `publicUrl` that will return the absolute url to the given path.

Now let's update our `createHttpApplication` file to get the address from it.

But before this, let's create a function to set the `baseUrl` variable, so we can use it in our project.

```ts
// src/utils/urls.ts
let baseUrl = '';

/**
 * Set base url
 */
export function setBaseUrl(url: string) {
  baseUrl = url;
}

  // ..
```

Now updating our `createHttpApplication` file:

```ts
// src/core/http/createHttpApplication.ts
import router from "core/router";
import { setBaseUrl } from "core/utils/urls";
import { httpConfig } from "./config";
import registerHttpPlugins from "./plugins";
import response from "./response";
import { getServer } from "./server";

export default async function createHttpApplication() {
  const server = getServer();

  await registerHttpPlugins();

  // call reset method on response object to response its state
  server.addHook("onResponse", response.reset.bind(response));

  router.scan(server);

  try {
    // 👇🏻 We can use the url of the server
    const baseUrl = await server.listen({
      port: httpConfig("port"),
      host: httpConfig("host"),
    });

    // update base url
    setBaseUrl(baseUrl);

    console.log(`Start browsing using ${baseUrl}`);
  } catch (err) {
    console.log(err);

    server.log.error(err);
    process.exit(1); // stop the process, exit with error
  }
}
```

I just added a lien after listening to the server to set the base url and changed the const name from `address` to `baseUrl` to be more clear.

## 🎨 Conclusion

## ☕♨️ Buy me a Coffee ♨️☕

If you enjoy my articles and see it useful to you, you may [buy me a coffee](https://www.paypal.com/paypalme/hassanzohdy), it will help me to keep going and keep creating more content.

## 🚀 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/pb2vmdfhGf) 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)

* [OOP In JS And TS From The Very Beginning](https://dev.to/hassanzohdy/jsts-oop-course-1-introduction-2ld)
* [ES6++: The Ultimate Guide to ES6 and Beyond](https://dev.to/hassanzohdy/es6-1-introduction-h3e)
* [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)