Upload Files with any Cloud Service with just 10 lines of code
Ivan Zaldivar
Posted on April 4, 2022
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 of the moment is in beta, please note that it is not ready for production yet, thanks for your understanding ️️❤️
Table of contents
🤔 What is a 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
- 🔭 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
- Have Node.js installed.
- Have VSCode (or your favorite Code Editor) installed.
⚙️ Setup
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
Create a src/index.ts and initialize app
import express from "express";
const app = express();
app.listen(3030, () => {
console.log("App execute in port:3030");
})
Setting service
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
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.
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 |
🔥 Conclusions
As you have just seen, 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
Posted on April 4, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
July 5, 2022