als-static-routes

1.2.0 • Public • Published

als-static-routes

als-static-routes is a Node.js library designed to simplify the process of setting up static routes for serving files in a web application. It's particularly useful for projects where you need to quickly and efficiently serve a variety of static resources, such as images, scripts, and stylesheets.

Installation

To install als-static-routes, use npm:

npm install als-static-routes

Usage

Initializing Handlers

To begin using als-static-routes, you need to initialize the handler creator. This is done by specifying the root directory (rootDir) of your project or the directory relative to which files will be served.

rootDir is a mandatory parameter and should be an absolute path.

Options include:

  • index (boolean, default: true): Automatically serve index.html files in directories.
  • download (boolean, default: false): Set the content disposition to attachment.
  • noType (string, default: 'text/plain'): Default MIME type for served files.
  • logger (function, default: console.error): Function to log errors.

If you provide an Express app instance, routes will be added using app.get.

Errors are thrown in the following cases:

  • Invalid URL path.
  • Path does not exist.
  • Issues with file reading or processing.

Example:

const initStatic = require('als-static-routes');
const express = require('express');
const app = express();

const rootDir = __dirname;

const options = {
   index:false, // don't use index.html on url without index.html
   download:false, // send files not for download
   noType:'text/plain',
   logger:console.log
}

const addStatic = initStatic(rootDir, options, app);

Using addStatic Function

The addStatic function allows you to set up static routes. Both url and path should be relative paths. The path can either point to a single file or a directory.

By default, options will use initialized values, but you can specify custom options if needed.

Errors are thrown if:

  • The path does not exist.
  • There are issues with file or directory processing.

Under the Hood

  • Utilizes als-store for caching files.
  • Checks the file type and sends data as a buffer or utf8 string.
  • Normalizes URLs using als-normalize-urlpath.

Practical Examples

const express = require('express');
const initStatic = require('als-static-routes');
const path = require('path');

const app = express();
const rootDir = path.join(__dirname, 'public');

// Initialize static route creator with default options
const addStatic = initStatic(rootDir, {}, app);

// Add a static route for '/images' directory
addStatic('/images', 'images');

// Add a static route for a single file
addStatic('/main.css', 'css/main.css');

addStatic('/some.pdf', 'docs',{download:true});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

This setup will serve all files in the /images directory and the main.css file from the /css directory, relative to the public folder in your project.

Package Sidebar

Install

npm i als-static-routes

Weekly Downloads

3

Version

1.2.0

License

MIT

Unpacked Size

25 kB

Total Files

17

Last publish

Collaborators

  • alexsorkin