Code Monkey home page Code Monkey logo

astro-fastify's Introduction

npm version number

@matthewp/astro-fastify

An fastify adapter to use in Astro projects. @matthewp/astro-fastify allows you to use fastify and Astro side-by-side, and deploy your apps to Node.js powered by a battle-tested HTTP server.

Unlike most adapters, @matthewp/astro-fastify also works in dev mode.

install

@matthewp/astro-fastify is needed in production so use --save.

npm install @matthewp/astro-fastify --save

Usage

@matthewp/astro-fastify is used like any other adapter for Astro. Import and use it in your astro.config.mjs file:

import fastify from '@matthewp/astro-fastify';

/** @type {import('astro').AstroUserConfig} */
export default {
  output: 'server',
  adapter: fastify({
    entry: new URL('./api/index.ts', import.meta.url)
  })
};

Options

entry

Specifies the entry point to define fastify routes and plugins. This module must export a default function that takes in the Fastify instance, where you can define routes and registry plugins.

api/index.ts

import type { DefineFastifyRoutes } from '@matthewp/astro-fastify';

const defineRoutes: DefineFastifyRoutes = (fastify) => {
  fastify.get('/api/todos', function(request, reply) {
    reply.send({
      todos: [
        { label: 'eat lunch' },
        { label: 'exercise' },
        { label: 'walk the dog' }
      ]
    });
  })
};

export default defineRoutes;

port

Specifies the port to use in production. Most hosts will set process.env.PORT and that will be used, so setting this option is unnecessary. If you do set port it will override host-specific config.

In development mode this option has no effect, as fastify runs on the same server as Astro.

logger

Specifies the Fastify logging options. See the Fastify docs to see the options. Note that these options are built into the production bundle, so options such as logger.stream do not work.

Note on route priority

Fastify runs in front of Astro's own routing, which means that any routes you define in fastify take priority over routes defined in Astro. So if, for example, you have conflicting routes the Astro route will never be hit.

Production usage

@matthewp/astro-fastify automatically configures Astro to build your fastify routes into your production build. Run your build as normal:

astro build

Or if you have an npm script:

npm run build

Which will create an entrypoint for your server, by default dist/server/entry.mjs. Running this file in Node.js automatically starts the fastify server:

node dist/server/entry.mjs

Configure your host to run this script. Assets and JavaScript are output to dist/client/assets/. You can configure your CDN to serve these files more efficiently and with long-lived cache headers.

License

BSD-2-Clause

astro-fastify's People

Contributors

matthewp avatar acecodept avatar jbanety avatar smnbbrv avatar sapphicmoe avatar juliancataldo avatar sashafklein avatar

Forkers

scutifer

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.