What is @types/send?
The @types/send package provides TypeScript type definitions for the 'send' package, which is a utility for streaming files from the file system as an HTTP response. It supports directory indexes, conditional GET requests, and other high-level features. The @types/send package itself does not implement these functionalities but provides type definitions to help TypeScript developers use the 'send' package more effectively by offering compile-time type checking and IntelliSense support in code editors.
What are @types/send's main functionalities?
Serving a file
This code demonstrates how to serve a file using the 'send' package. It creates an HTTP server that listens on port 3000 and uses the 'send' function to stream a file from the file system to the response. The @types/send package provides type definitions for this usage.
import send from 'send';
import http from 'http';
http.createServer((req, res) => {
send(req, '/path/to/file.txt').pipe(res);
}).listen(3000);
Setting custom headers
This example shows how to set custom HTTP headers before sending the file. It listens for the 'headers' event and then sets the 'Cache-Control' header. The @types/send package provides type definitions for these event handlers and method chaining.
import send from 'send';
import http from 'http';
http.createServer((req, res) => {
send(req, '/path/to/file.txt')
.on('headers', (res, path, stat) => {
res.setHeader('Cache-Control', 'public, max-age=3600');
})
.pipe(res);
}).listen(3000);
Other packages similar to @types/send
express
Express is a web application framework for Node.js, designed for building web applications and APIs. It includes similar file serving capabilities through the 'express.static' middleware. While 'send' focuses solely on streaming files, Express provides a broader range of web server functionalities. The @types/express package offers TypeScript definitions for Express.
koa-send
koa-send is a file serving utility for Koa, a web framework for Node.js. It is similar to 'send' but designed to work within the Koa ecosystem. koa-send supports serving files, setting custom headers, and more, tailored for Koa's middleware architecture. The @types/koa-send package provides TypeScript definitions for koa-send.