Skip to content
Account

File Upload Server

This NodeJS application allows files to be uploaded to S3.

How the Application Works

The server accepts files at the /upload route, creates FormData with the received file using the multer library, and uploads the file to the Default Uploader. The server then responds with information about the uploaded files from the Default Uploader.

This is a classic example for scenarios where your users need to upload photos, videos, or documents.

Server Implementation

Create the application

create nodejs app
mkdir myuploaderserver && cd myuploaderserver && npm init -y

Create an index.js file; your file structure should look like this.

  • Directorymyuploaderserver/
    • package.json
    • index.js

Copy the code below into index.js

express server
const axios = require('axios');
const express = require('express');
const multer = require('multer');
const FormData = require('form-data');
const app = express();
const port = 3000;
const url = 'https://api.defaultuploader.com/v1/upload';
const token = 'YOUR_SECRET_CLIENT_TOKEN'; // Заменить на ваш токен!
const storage = multer.memoryStorage();
const upload = multer({ storage });
app.post('/upload', upload.any('file'), async (req, res) => {
try {
const form = new FormData();
for (const file of req.files) {
form.append('file', file.buffer, {
filename: file.originalname,
});
}
const response = await axios.post(url, form, {
headers: {
...form.getHeaders(),
Authorization: `${token}`,
},
});
console.log('File uploaded successfully!');
console.log('Response:', response.data);
res.status(200).json({ data: response.data });
} catch (error) {
console.error('Error uploading file:', error.message);
res.status(500).json({ error: 'File upload failed' });
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});

Install dependencies

dependencies
npm i axios express multer form-data

Start the server

express server
node index.js

Testing

Use Postman to send a multipart/form-data request to http://localhost:3000/upload with an attached file. Don’t forget to include your SECRET_CLIENT_TOKEN in the code.

Or with CURL

CURL
curl --location 'http://localhost:3000/upload' --header 'Content-Type: image/svg+xml' --header 'Authorization: SECRET_CLIENT_TOKEN' --form 'image=@"/C:/Users/user/Desktop/image.jpg"'