# ucdn **Repository Path**: mirrors_WebReflection/ucdn ## Basic Information - **Project Name**: ucdn - **Description**: A µcompress based CDN utility, compatible with both Express and native http module - **Primary Language**: Unknown - **License**: ISC - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-08-19 - **Last Updated**: 2025-12-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # µcdn [](https://travis-ci.com/WebReflection/ucdn) [](https://coveralls.io/github/WebReflection/ucdn?branch=master)  **Social Media Photo by [Eduardo Casajús Gorostiaga](https://unsplash.com/@eduardo_cg) on [Unsplash](https://unsplash.com/)** A [ucompress](https://github.com/WebReflection/ucompress#readme) based utility that accepts a configuration object with a `source` path, an optional `dest`, which fallbacks to the _temp_ folder, plus eventually extra `headers` property to pollute headers via `allow-origin` among other details. ### 📣 Community Announcement Please ask questions in the [dedicated forum](https://webreflection.boards.net/) to help the community around this project grow ♥ --- ## 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_. ```js import {createServer} from 'http'; import {join} from 'path'; import umeta from 'umeta'; const {dirName} = umeta(import.meta); import ucdn from 'ucdn'; const callback = cdn({ cacheTimeout: 1000 * 60, // 1 min cache source: join(dirName, 'source'), // dest: join(dirName, 'dest') }); 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. ```js const {join} = require('path'); const express = require('express'); const ucdn = require('ucdn'); const app = express(); app.use(ucdn({ source: join(__dirname, 'source'), dest: join(__dirname, 'dest') })); app.get('/unknown', (req, res) => { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('OK'); }); app.listen(8080); ``` ## As binary file It is possible to bootstrap a micro CDN right away via `npx ucdn`. Pass `--help` to see options. #### The `--verbose` output If started via `--verbose` flag, each request will be logged producing the following output example: > **200** XXms /full/path.html.gzip > > **404** /full/nope.html > > **200** /favicon.ico > > **404** /full/nope.html > > **304** /full/path.html.gzip > > **500** /full/error-during-compression Please note that **200** is the file status in the *cdn*, not the response status for the browser. The status indeed indicates that the file wasn't known/compressed yet, and it took *Xms* to be generated. On the other hand, whenever a file was already known, it will be served like a **304** as the *cdn* didn't need to perform any operation. Basically, the status reflects the *cdn* and *not* whenever a browser is requesting new content or not. ### About `API` If _ucdn_ is started with an `--api ./path` flag, files in that folder will be used as fallback. The _API_ folder does not need to be reachable, or included, within static assets (_source_). ```js // @file ./api/hello.js // @start ucdn --api ./api --source ./public const headers = {'content-type': 'text/html;charset=utf-8'}; // export a function that will receive // the request and response from the server module.exports = (req, res) => { res.writeHead(200, headers); res.end('