Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

serve-static

Package Overview
Dependencies
Maintainers
2
Versions
66
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

serve-static

Serve static files

  • 2.0.0-beta.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
36M
increased by3.35%
Maintainers
2
Weekly downloads
 
Created

What is serve-static?

The serve-static npm package is used to serve static files such as images, CSS files, and JavaScript files. It is built on top of the core 'http' module in Node.js and provides a middleware that can be used with frameworks like Express to serve files from a directory in the file system.

What are serve-static's main functionalities?

Basic static file serving

This code sample demonstrates how to serve static files from a directory named 'public'. When a request is made to the server, it will look for files in this directory to serve.

const express = require('express');
const serveStatic = require('serve-static');

const app = express();

app.use(serveStatic('public'));

app.listen(3000);

Customizing cache control

This code sample shows how to customize cache control headers for the files served. The 'maxAge' option sets the cache control max-age directive in seconds, and the 'setHeaders' function allows for further customization of the headers.

const express = require('express');
const serveStatic = require('serve-static');

const app = express();

app.use(serveStatic('public', {
  maxAge: '1d',
  setHeaders: function (res, path) {
    res.setHeader('Cache-Control', 'public, max-age=86400')
  }
}));

app.listen(3000);

Serving files from multiple directories

This code sample demonstrates how to serve static files from multiple directories. The first 'serveStatic' serves files from the 'public' directory, while the second one serves files from the 'media' directory under the '/media' path.

const express = require('express');
const serveStatic = require('serve-static');

const app = express();

app.use(serveStatic('public'));
app.use('/media', serveStatic('media'));

app.listen(3000);

Other packages similar to serve-static

FAQs

Package last updated on 21 Mar 2024

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc