Awesome
<em>µ</em>cdn-utils
Basic µcdn utilities in order to just serve files pre generated via µcompress.
The only interesting utility to use is likely the serve(path[, cacheTimeout])
one, as the rest is mostly needed for internal purposes in µcdn module or in here.
Please Note
This module assumes all files already have associated headers. If you don't have those headers files, please use ucompress.createHeaders(path) before serving via serve(path[, cacheTimeout])
.
Example
The following example will serve every file within any folder in the source
directory, automatically optimizing on demand all operations, including the creation of brotli, gzip, or deflate.
import {createServer} from 'http';
import {serve} from 'ucdn-utils';
import umeta from 'umeta';
const {dirName} = umeta(import.meta);
const callback = serve(dirName + '/source', 60000);
createServer(callback).listen(8080);
The callback works with Express too, and similar modules, where all non existent files in the source folder will be ignored, and anything else will execute regularly.
const {join} = require('path');
const express = require('express');
const {serve} = require('ucdn-utils');
const app = express();
app.use(serve(join(__dirname, 'source')));
app.get('/unknown', (req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('OK');
});
app.listen(8080);