Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
koa-static
Advanced tools
The koa-static package is a middleware for Koa, a popular Node.js web framework. It serves static files such as HTML, CSS, JavaScript, and images from a specified directory. This is useful for serving front-end assets in a web application.
Serve Static Files
This feature allows you to serve static files from a specified directory. In this example, files from the 'public' directory will be served.
const Koa = require('koa');
const serve = require('koa-static');
const app = new Koa();
// Serve files from the 'public' directory
app.use(serve('./public'));
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Custom Options
This feature allows you to customize the behavior of the static file serving. You can set options like cache duration, serving hidden files, and specifying a default file.
const Koa = require('koa');
const serve = require('koa-static');
const app = new Koa();
// Serve files from the 'public' directory with custom options
app.use(serve('./public', {
maxage: 86400000, // Cache files for 1 day
hidden: true, // Allow hidden files to be served
index: 'index.html' // Default file to serve
}));
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
The serve-static package is a middleware for Express, another popular Node.js web framework. It serves static files similarly to koa-static but is designed for use with Express. It offers similar functionalities such as serving files from a directory and customizing options like cache control.
The static-server package is a simple, standalone HTTP server for serving static files. Unlike koa-static, it is not a middleware and does not require a web framework like Koa or Express. It is useful for quickly serving static files without setting up a full web server.
The http-server package is a simple, zero-configuration command-line HTTP server. It is used to serve static files and is often used for development and testing purposes. Unlike koa-static, it is not a middleware and does not integrate with web frameworks.
Static file serving middleware.
$ npm install koa-static
maxage
Browser cache max-age in milliseconds. defaults to 0hidden
Allow transfer of hidden files. defaults to falseindex
Default file name, defaults to 'index.html'defer
If true, serves after yield next
, allowing any downstream middleware to respond first.var serve = require('koa-static');
var koa = require('koa');
var app = koa();
// $ GET /package.json
app.use(serve('.'));
// $ GET /hello.txt
app.use(serve('test/fixtures'));
// or use absolute paths
app.use(serve(__dirname + '/test/fixtures'));
app.listen(3000);
console.log('listening on port 3000');
MIT
FAQs
Static file serving middleware for koa
The npm package koa-static receives a total of 707,724 weekly downloads. As such, koa-static popularity was classified as popular.
We found that koa-static demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 10 open source maintainers collaborating on the project.
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.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.