A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/koajs/send below:

GitHub - koajs/send: Transfer static files

Koa static file serving middleware.

Notice: We recommend using @koa/send for installation, as both koa-send and @koa/send refer to the same module. However, in our next major versions bumps, we will deprecate koa-send and only maintain the module under @koa/send.

Note that root is required, defaults to '' and will be resolved, removing the leading / to make the path relative 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(async (ctx) => {
  await send(ctx, ctx.path, { root: __dirname + "/public" });
});

To serve developer specified files:

app.use(async (ctx) => {
  await send(ctx, "path/to/my.js");
});

The function is called as fn(res, path, stats), where the arguments are:

You should only use the setHeaders option when you wish to edit the Cache-Control or Last-Modified headers, because doing it before is useless (it's overwritten by send), and doing it after is too late because the headers are already sent.

If you want to edit any other header, simply set them before calling send.

const send = require("@koa/send");
const Koa = require("koa");
const app = new Koa();

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

app.use(async (ctx) => {
  if ("/" == ctx.path) return (ctx.body = "Try GET /package.json");
  await send(ctx, ctx.path);
});

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

MIT


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4