1.0.5 • Published 4 years ago
async-cdn v1.0.5
async-cdn
An Nginx style CDN to send static files asynchronously
methods
readDir('directoryLocation', directoryTraversalProtection, callback())
sendFile('directoryLocation', directoryTraversalProtection, callback())
options
- directory location: string
- a file or folder location
- directory traveral protection: boolean
- if you want the (end) user to be able to navigate backwards through the filesystem with '../../etc/hosts'
- callback: function()
- callback function
example
const express = require('express');
const app = express();
const CDN = require('async-cdn');
app.get('/', (req, res) => {
CDN.readDir('./', true, (err, data) => {
if (err) return res.json(err);
res.json(data);
});
});
app.get('/:folderOrFile', (req, res) => {
if (req.params.folderOrFile.includes('.')) {
req.params.folderOrFile = `./${req.params.folderOrFile}`;
CDN.sendFile(req.params.folderOrFile, true, (err, data) => {
if (err) return res.json(err);
res.send(data);
});
} else if (!req.params.folderOrFile.includes('.')) {
req.params.folderOrFile = `./${req.params.folderOrFile}`;
CDN.readDir(req.params.folderOrFile, true, (err, data) => {
if (err) return res.json(err);
res.send(data);
});
}
});
app.listen(3000);
license
Public-Domain