Giter Site home page Giter Site logo

fastify-edge's Introduction

fastify-edge

An experimental lightweight worker version of Fastify.

Currently Cloudflare Workers and Bun are supported.

Install

npm i fastify-edge --save

Usage: Bun

import FastifyEdge from 'fastify-edge/bun'

const app = FastifyEdge();

app.get('/', (_, reply) => {
  reply.send('Hello World')
})

export default app;

See examples/bun.

Usage: Cloudflare Workers

import FastifyEdge from 'fastify-edge'

const app = FastifyEdge()

app.get('/', (_, reply) => {
  reply.send('Hello World')
})

See examples/cloudflare with miniflare.

Advanced Example

app.addHook('onSend', (req, reply, payload) => {
  if (req.url === '/') {
    return `${payload} World!`
  }
})

app.get('/redirect', (_, reply) => {
  reply.redirect('/')
})

app.get('/route-hook', {
  onRequest (_, reply) {
    reply.send('<b>Content from onRequest hook</b>')
  },
  handler (_, reply) {
    reply.type('text/html')
  }
})

Supported APIs

Server

  • app.addHook(hook, function)
  • app.route(settings)
  • app.get(path, handlerOrSettings)
  • app.post(path, handlerOrSettings)
  • app.put(path, handlerOrSettings)
  • app.delete(path, handlerOrSettings)
  • app.options(path, handlerOrSettings)

Request

req.url

Returns the request URL path (URL.pathname + URL.search).

req.origin

Returns the request URL origin (e.g., http://localhost:3000).

req.hostname

Returns the request URL hostname (e.g., localhost).

req.protocol

Returns the request URL protocol (e.g., http or https).

req.query

Maps to the fetch request URL's searchParams object through a Proxy.

req.body

The consumed body following the parsing pattern from this example.

req.params

The parsed route params from the internal Radix-tree router, radix3.

req.headers

Maps to the fetch request headers object through a Proxy.

req.raw

The raw fetch request object.

Reply

reply.code(code)

Sets the fetch response status property.

reply.header(key, value)

Adds an individual header to the fetch response headers object.

reply.headers(object)

Adds multiple headers to the fetch response headers object.

reply.getHeader(key)

Retrieves an individual header from fetch response headers object.

reply.getHeaders()

Retrieves all headers from fetch response headers object.

reply.removeHeader(key)

Remove an individual header from fetch response headers object.

reply.hasHeader(header)

Asserts presence of an individual header in the fetch response headers object.

reply.redirect(code, dest)
reply.redirect(dest)

Sets the status and redirect location for the fetch response object.
Defaults to the HTTP 302 Found response code.

reply.type(contentType)

Sets the content-type header for the fetch response object.

reply.send(data)

Sets the body for the fetch response object.

Can be a string, an object, a buffer or a stream.

Objects are automatically serialized as JSON.

Supported hooks

The original Fastify onRequest, onSend and onResponse are supported.

Diverging from Fastify, they're all treated as async functions.

They can be set at the global and route levels.

Limitations

  • No support for preHandler, preParsing and preValdation hooks.
  • No support for Fastify's plugin system (yet).
  • No support for Fastify's logging and validation facilities.
  • Still heavily experimental, more equivalent APIs coming soon.

fastify-edge's People

Contributors

davidmeirlevy avatar galvez avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  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.