Socket
Socket
Sign inDemoInstall

koa-send

Package Overview
Dependencies
2
Maintainers
7
Versions
28
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    koa-send

Transfer static files


Version published
Weekly downloads
751K
increased by3.46%
Maintainers
7
Created
Weekly downloads
 

Package description

What is koa-send?

The koa-send package is a utility for Koa applications that allows you to serve static files. It is commonly used to serve files such as HTML, CSS, JavaScript, images, and other static assets from a directory on the server.

What are koa-send's main functionalities?

Serving Static Files

This feature allows you to serve static files from a specified path. In this example, when a request is made to '/static', the server responds with the file located at 'path/to/static/file.html'.

const Koa = require('koa');
const send = require('koa-send');
const app = new Koa();

app.use(async (ctx) => {
  if (ctx.path === '/static') {
    await send(ctx, 'path/to/static/file.html');
  }
});

app.listen(3000);

Serving Files from a Directory

This feature allows you to serve files from a directory. In this example, any request path will be mapped to a file in the 'public' directory relative to the current directory.

const Koa = require('koa');
const send = require('koa-send');
const app = new Koa();

app.use(async (ctx) => {
  await send(ctx, ctx.path, { root: __dirname + '/public' });
});

app.listen(3000);

Setting Cache Control Headers

This feature allows you to set cache control headers for the served files. In this example, the 'maxage' option is set to 1 hour, which will set the 'Cache-Control' header to cache the file for 1 hour.

const Koa = require('koa');
const send = require('koa-send');
const app = new Koa();

app.use(async (ctx) => {
  await send(ctx, 'path/to/static/file.html', { maxage: 1000 * 60 * 60 }); // 1 hour
});

app.listen(3000);

Other packages similar to koa-send

Readme

Source

koa-send Build Status

Static file serving middleware.

Installation

$ npm install koa-send

Options

  • maxage Browser cache max-age in milliseconds. defaults to 0
  • hidden Allow transfer of hidden files. defaults to false
  • root Root directory to restrict file access

Root path

Note that when root is not used you MUST provide an absolute path, and this path must not contain "..", protecting developers from concatenating user input. If you plan on serving files based on user input supply a root directory from which to serve from.

For example to serve files from ./public:

app.use(function *(){
  yield send(this, this.path, { root: __dirname + '/public' });
})

To serve developer specified files:

app.use(function *(){
  yield send(this, 'path/to/my.js');
})

Example

var send = require('koa-send');
var koa = require('koa');
var app = koa();

// $ GET /package.json
// $ GET /

app.use(function *(){
  if ('/' == this.path) return this.body = 'Try GET /package.json';
  yield send(this, __dirname + '/package.json');
})

app.listen(3000);
console.log('listening on port 3000');

License

MIT

Keywords

FAQs

Last updated on 23 Jul 2014

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc