New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@ceicc/range

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ceicc/range

http range request handler

  • 2.2.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

range

static files request handler

Installation

npm i @ceicc/range

Usage

start by importing http and range

const
http = require("http"),
range = require("@ceicc/range");

Then make a simple server that responds with a file based on the requested path

http.createServer((req, res) => {

 range(__dirname + req.url, res).catch(console.error)

}).listen(2000);

Parameters

  1. file path.
  2. the response object.
  3. optional object

Options Object

  1. maxAge max age of caching in seconds - default 0

  2. etag add Etag header - default true

  3. lastModified add last-modified header - default true

  4. conditional whether to respect conditional requests or not - default false
    if true, the headers object is required.

  5. range accept range request - default false
    if true, the headers object is required.

  6. headers the request headers object req.headers
    if range and/or conditionalRequest are true, then the headers object is required.
    you can pass the whole headers object, or only the conditional and range headers.

  7. notFound handler for non existing files
    notFound: false - a rejection will be thrown (default).
    notFound: true - empty body with response code '404' will be sent.
    notFound: <string> - send a file with response code '404', the given string is the path to file.
    if the path doesn't led to a file, a rejection will be thrown.

  8. implicitIndex Check for index files if the request path is a directory. default: false
    Pass an array of extensions to check against. e.g. ["html", "css"]
    Or simply pass true to check for html extension only.

Resolves

the response status code

Rejects

'File Not Found' error.

Any other unexpected error

Real World Example

const
express = require("express"),
range = require("@ceicc/range"),
app = express();

app.get('/', (req, res, next) => range('./public/index.html', res).catch(next));

app.get('/public/*', (req, res, next) => {
 range('.' + req.path, res, {
   headers: req.headers,
   range: true,
   conditional: true,
   maxAge: 2592000, // 30 Days
   notFound: './test/public/404.html',
 }).catch(next);
});

app.use((err, req, res, next) => {
 console.dir(err);
 if (!res.headersSent)
   res.sendStatus(500);
});

app.listen(2000);

Keywords

FAQs

Package last updated on 05 Jun 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc