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

@fastify/static

Package Overview
Dependencies
Maintainers
20
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fastify/static

Plugin for serving static files as fast as possible.

  • 7.0.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
20
Created

What is @fastify/static?

@fastify/static is a Fastify plugin for serving static files, such as HTML, CSS, JavaScript, and images. It is designed to be highly performant and easy to use, leveraging the speed and efficiency of the Fastify framework.

What are @fastify/static's main functionalities?

Serve Static Files

This feature allows you to serve static files from a specified directory. In this example, files from the 'public' directory are served with the '/public/' prefix.

const fastify = require('fastify')();
const path = require('path');

fastify.register(require('@fastify/static'), {
  root: path.join(__dirname, 'public'),
  prefix: '/public/', // optional: default is '/'
});

fastify.listen(3000, err => {
  if (err) throw err;
  console.log('Server listening on http://localhost:3000');
});

Serve Single Page Applications (SPA)

This feature is useful for serving Single Page Applications (SPA). When a route is not found, it serves the 'index.html' file, allowing the client-side router to handle the navigation.

const fastify = require('fastify')();
const path = require('path');

fastify.register(require('@fastify/static'), {
  root: path.join(__dirname, 'public'),
  wildcard: false
});

fastify.setNotFoundHandler((request, reply) => {
  reply.sendFile('index.html');
});

fastify.listen(3000, err => {
  if (err) throw err;
  console.log('Server listening on http://localhost:3000');
});

Serve Files with Custom Headers

This feature allows you to set custom headers for the served files. In this example, a custom header 'X-Custom-Header' is added to the response.

const fastify = require('fastify')();
const path = require('path');

fastify.register(require('@fastify/static'), {
  root: path.join(__dirname, 'public'),
  setHeaders: (res, path, stat) => {
    res.setHeader('X-Custom-Header', 'my-custom-header');
  }
});

fastify.listen(3000, err => {
  if (err) throw err;
  console.log('Server listening on http://localhost:3000');
});

Other packages similar to @fastify/static

Keywords

FAQs

Package last updated on 01 Feb 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