Publish your own packages to the world's most popular software ecosystem with npm Pro.Get started »

async-cdn

1.0.4 • Public • Published

async-cdn

An Nginx style CDN to send static files asynchronously

methods

  • readDir('directoryLocation', directoryTraversalProtection, callback())

  • sendFile('directoryLocation', directoryTraversalProtection, callback())

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

Keywords

none

Install

npm i [email protected]

Version

1.0.4

License

Public-Domain

Unpacked Size

5.21 kB

Total Files

4

Last publish

Collaborators

  • avatar