Upload Files with any Cloud Service with just 10 lines of code
Tags: opensource, javascript, webdev, beginners
Published: 2022-04-04 15:05:07

Hi developers! After months of not posting anything, here I am again, and this time with an open source project that I've been working on for months. Well let's get started.

> ⚠️ [Filesrocket](https://github.com/Filesrocket/filesrocket) of the moment is in *beta*, please note that it is not ready for production yet, thanks for your understanding ️️❤️

## Table of contents

1. [What is a Filesrocket](#introduction)
2. [Features](#features)
3. [Prerequisites](#prerequisites)
4. [Setup](#setup)
5. [Basic usage](#basic-usage)
6. [Conclusions](#conclusions)

## 🤔 What is a Filesrocket? <a name="introduction"></a>

[Filesrocket](https://github.com/filesrocket/filesrocket) is an packages of Node.js that allows management your files through strategies that help manage with any cloud services (Local, Cloudinary, AWS).

## 🌈 Features <a name="features"></a>

- 🔭 Manage your files with multiple cloud storage services (Cloudinary, Amazon S3, Local)
- ✅ Validations (validate extensions, file sizes and more)
- ⚡ Easy to configure and integrate
- 🛡️ Written in Typescript
- 👨🏻‍💻 Community driven

## 📝 Prerequisites <a name="prerequisites"></a>
- Have Node.js installed.
- Have VSCode (or your favorite Code Editor) installed.

## ⚙️ Setup <a name="setup"></a>
Now we are going to add some configurations for our node.

Creating project.

```
mkdir filesrocket-app-example

cd filesrocket-app-example
```

Initialize project.

```
npm i typescript ts-node -g

npm init -y

tsc --init
```

Now is necessary installed the following packages.

```
npm i express filesrocket filesrocket-local

npm i @types/express -D
```

Perfect, with this we already have everything we need.

## 🧑‍💻 Basic Usage <a name="basic-usage"></a>

Create a **src/index.ts** and initialize app

```ts
import express from "express";

const app = express();

app.listen(3030, () => {
  console.log("App execute in port:3030");
})
```

Setting service

```ts
import { Filesrocket } from "filesrocket";

import { LocalFileService } from "filesrocket-local";
 
// Initialize
const filesrocket = new Filesrocket();

// Config your service
const service = new LocalFileService({
  pagination: { default: 15, max: 50 },
  host: "http://localhost:3030",
  directory: "uploads",
});
 
// We register the service
filesrocket.register("local", service);
```

Register your endpoints

```ts
const controller = filesrocket.controller("local");
 
// Create/Upload files.
app.post("/files", async (req, res) => {
  const files = await controller.create(req, {});
  res.status(200).json(files);
})
 
// List files.
app.get("/files", async (req, res) => {
  const query = req.query;

  const files = await controller.list(query);
  
  res.status(200).json(files);
})
 
// Remove files.
app.delete("/files", async (req, res) => {
  const query = req.query;
  const { id } = query;

  const files = await controller.remove(id, query)
  
  res.status(200).json(files)
})
```

Expose static files.

```ts
app.use("/uploads", express.static(path.resolve("uploads")));
```

Execute server

```
ts-node src/index.ts
```

With this example you can manage your files locally.

## Examples 👀
To make it easier to understand the tool we have created several repositories with the most popular frameworks so that you can clone them on your computer and play with them.

| Framework | Repository |
| --------- | ---------- |
| Express | [filesrocket-express-app](https://github.com/Filesrocket/filesrocket-express-app) |

## 🔥 Conclusions <a name="conclusions"></a>
As you have just seen, [Filesrocket](https://github.com/filesrocket/filesrocket) is a tool that allows you not only to upload your files like *multer*, *express-fileupload* and others, but it also allows you to consult, delete, provides you with a pre-formed URL and other metadata. This was a short article, if you want to learn more in depth we recommend you visit the official documentation.

## ❤️ Follow me

- [Twitter](https://twitter.com/thebug404)
- [Github](https://github.com/thebug404)