What is @types/koa-send?
@types/koa-send provides TypeScript definitions for the koa-send package, which is used to serve static files in a Koa application.
What are @types/koa-send's main functionalities?
Serving Static Files
This feature allows you to serve static files from a specified path. In this example, when the user accesses the '/static' route, the server will respond with the file located at 'path/to/file.txt'.
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/file.txt');
}
});
app.listen(3000);
Setting Root Directory
This feature allows you to set a root directory for serving static files. In this example, the server will look for 'file.txt' in the 'public' directory located in the current directory.
const Koa = require('koa');
const send = require('koa-send');
const app = new Koa();
app.use(async (ctx) => {
if (ctx.path === '/static') {
await send(ctx, 'file.txt', { root: __dirname + '/public' });
}
});
app.listen(3000);
Setting Cache Control
This feature allows you to set cache control headers for the served files. In this example, the 'maxAge' option is set to 1 hour, meaning the file will be cached for 1 hour.
const Koa = require('koa');
const send = require('koa-send');
const app = new Koa();
app.use(async (ctx) => {
if (ctx.path === '/static') {
await send(ctx, 'file.txt', { maxAge: 1000 * 60 * 60 }); // 1 hour
}
});
app.listen(3000);
Other packages similar to @types/koa-send
@types/koa-static
@types/koa-static provides TypeScript definitions for the koa-static package, which is another middleware for serving static files in a Koa application. Unlike koa-send, koa-static is specifically designed for serving static files and directories, making it simpler to use for that purpose.
@types/serve-static
@types/serve-static provides TypeScript definitions for the serve-static package, which is used to serve static files in Express applications. While it is designed for Express, it can be used with Koa through the koa-connect package, offering similar functionality to koa-send.