server-uploader
v1.0.4
Published
Uploader File To URL.
Downloads
13
Maintainers
Readme
Readme - Server Uploader
Description
This module is used to upload files to a server using Node.js.
Installation
- Install the package using the following command:
npm install server-uploader
- Once the package is installed, import the module in the required JavaScript file:
const { uploader } = require('server-uploader');
Usage
To upload a file, follow these steps:
- Read the file into buffer using
fs.readFileSync()
:
const fs = require('fs');
const buffer = fs.readFileSync('/path/to/file.jpg');
- Use
uploader()
to upload the file:
uploader(buffer)
.then(url => {
console.log('Done:', url);
})
.catch(error => {
console.error('Error:', error);
});
For reference, here are examples of using node-fetch
, axios
, and fs
to read a file into buffer before uploading it using server-uploader
.
Using node-fetch
for Buffer
const fetch = require('node-fetch');
const { uploader } = require('server-uploader');
let data = await fetch('https://example.com/path/to/file.jpg')
.then(response => response.buffer())
.then(buffer => {
uploader(buffer)
.then(url => {
console.log('Done:', url);
})
.catch(error => {
console.error('Error:', error);
});
})
.catch(error => {
console.error('Error:', error);
});
Using axios
for Buffer
const axios = require('axios');
const { uploader } = require('server-uploader');
let data = await axios.get('https://example.com/path/to/file.jpg', { responseType: 'arraybuffer' })
.then(response => {
uploader(response.data)
.then(url => {
console.log('Done:', url);
})
.catch(error => {
console.error('Error:', error);
});
})
.catch(error => {
console.error('Error:', error);
});
Credits
This module was developed by BOTCAHX.
License
This module is licensed under the MIT License.