Coder Social home page Coder Social logo

leorossi / fastify-static Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fastify/fastify-static

0.0 1.0 0.0 117 KB

Plugin for serving static file as fast as possible

License: MIT License

JavaScript 97.38% HTML 0.71% CSS 0.21% TypeScript 1.69%

fastify-static's Introduction

fastify-static

Build Status Greenkeeper badge Known Vulnerabilities

Plugin for serving static files as fast as possible. Supports Fastify versions >=2.0.0.

Please refer to this branch and related versions for Fastify ^1.11.0 compatibility.

Install

npm install --save fastify-static

Usage

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

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

fastify.get('/another/path', function (req, reply) {
  reply.sendFile('myHtml.html') // serving path.join(__dirname, 'public', 'myHtml.html') directly
})

fastify.get('/path/with/different/root', function (req, reply) {
  reply.sendFile('myHtml.html', path.join(__dirname, 'build')) // serving a file from a different root location
})

Options

root (required)

The absolute path of the directory that contains the files to serve. The file to serve will be determined by combining req.url with the provided root directory.

prefix

Default: '/'

A URL path prefix used to create a virtual mount path for the static directory.

schemaHide

Default: true

A flag that define if the fastify route hide-schema attribute is hidden or not

setHeaders

Default: undefined

A function to set custom headers on the response. Alterations to the headers must be done synchronously. The function is called as fn(res, path, stat), where the arguments are:

  • res The response object.
  • path The path of the file that is being sent.
  • stat The stat object of the file that is being sent.

send Options

The following options are also supported and will be passed directly to the send module:

redirect

Default: false

If set to true, fastify-static redirects to the directory with a trailing slash.

This option cannot be set to true with wildcard set to false on a server with ignoreTrailingSlash set to true.

If this option is set to false, then requesting directories without trailing slash will trigger your app's 404 handler using reply.callNotFound().

wildcard

Default: true

If set to true, fastify-static adds a wildcard route to serve files. If set to false, fastify-static globs the filesystem for all defined files in the served folder (${root}/**/*), and just creates the routes needed for those. If set to a glob string pattern, fastify-static will use the provided string when globing the filesystem (${root}/${wildcard}).

The default options of https://www.npmjs.com/package/glob are applied for getting the file list.

This option cannot be set to false with redirect set to true on a server with ignoreTrailingSlash set to true.

Disable serving

If you'd just like to use the reply decorator and not serve whole directories automatically, you can simply pass the option { serve: false }. This will prevent the plugin from serving everything under root.

Disabling reply decorator

The reply object is decorated with a sendFile function by default. If you want to disable this, pass the option { decorateReply: false }. If fastify-static is registers to multiple prefixes in the same route only one can initialize reply decorators.

Handling 404s

If a request matches the URL prefix but a file cannot be found for the request, Fastify's 404 handler will be called. You can set a custom 404 handler with fastify.setNotFoundHandler().

Handling Errors

If an error occurs while trying to send a file, the error will be passed to Fastify's error handler. You can set a custom error handler with fastify.setErrorHandler().

Payload stream.filename

If you need to access the filename inside the onSend hook, you can use payload.filename.

fastify.addHook('onSend', function (req, reply, payload, next) {
  console.log(payload.filename)
  next()
})

License

Licensed under MIT

fastify-static's People

Contributors

allevo avatar anderscan avatar cemremengu avatar chris-shaw-2011 avatar coreyfarrell avatar disintegrator avatar evanshortiss avatar gj avatar greenkeeper[bot] avatar hfjallemark avatar jannikkeye avatar jsardev avatar jsumners avatar lependu avatar mcollina avatar nwoltman avatar orblazer avatar patoi avatar sebdeckers avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.