74-Nodejs Course 2023: Break IV: Paths Functions
Tags: node, typescript, mongodb, fastify
Published: 2022-11-29 15:35:05

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

Let's move to another part, `paths` we already have `rootPath` function utility which returns the root path of current project, but we need to add more functions to it, so we can use it in our project.

## Paths functions

Here are our functions that we need to have so far to be used easier later.

* `storagePath`: Receives a relative path to the storage folder and returns the absolute path to it.
* `uploadsPath`: Receives a relative path to the uploads folder and returns the absolute path to it.
* `publicPath`: Receives a relative path to the public folder and returns the absolute path to it.
* `cachePath`: Receives a relative path to the cache folder and returns the absolute path to it.

Seems easy, right? yup, no tricks in this article, simple and straight forward.

## Implementation

Now create `paths.ts` file inside `src/utils` folder, if the folder doesn't exist, create it.

```ts
// src/utils/paths.ts
import { rootPath } from "@mongez/node";

/**
 * Get the absolute path to the storage folder to the given path
 *
 * If no path is given, it will return the absolute path to the storage folder
 */
export function storagePath(relativePath = "") {
  return rootPath("storage", relativePath);
}

/**
 * Get the absolute path to the uploads folder to the given path
 *
 * If no path is given, it will return the absolute path to the uploads folder
 */
export function uploadsPath(relativePath = "") {
  return rootPath("storage", "uploads", relativePath);
}

/**
 * Get the absolute path to the public folder to the given path
 *
 * If no path is given, it will return the absolute path to the public folder
 */
export function publicPath(relativePath = "") {
  return rootPath("public", relativePath);
}

/**
 * Get the absolute path to the cache folder to the given path
 *
 * If no path is given, it will return the absolute path to the cache folder
 */
export function cachePath(relativePath = "") {
  return rootPath("storage", "cache", relativePath);
}
```

Now we created the functions, but let's see why would we need these functions, let's start with the `storage` directory.

## Storage directory

The `storage` directory is used to store all files that need to be `stored`, such as any uploaded files, caches and any files that needs to be hidden in some way.

So basically this folder stores anything that is not a `code`.

## Uploads directory

The `uploads` directory is used to store all uploaded files, such as images, videos, documents, etc.

This folder will be located in the `storage` directory but we created a function to get the absolute path to it.

## Public directory

The `public` directory is used to store all files that are public, such as images, videos, documents, etc.

We can store it in `storage` directory, or just create a `public` directory and store it there, it's up to you.

## Cache directory

This directory stores all caches, such as images, html content (Generated pages for example) and so on.

## 🎨 Conclusion

We just created a utility file that is called `paths` to add our functions to it, so we can use it later in our project, these functions are used to get the absolute path to the storage, uploads, public and cache directories.

## ☕♨️ 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)