npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gatsby-plugin-nodejs

v0.7.0

Published

Gatsby plugin for integration with Node.js servers.

Downloads

988

Readme

About

gatsby-plugin-nodejs gives you a way to integrate your Gatsby site with a custom Node.js server. Use your favorite backend framework and set up your Gatsby.js site normally - the plugin will take care of everything:

  • Supported frameworks:
    • Express
    • Fastify 2.x
  • Serving built site
  • 404 page middleware
  • Gatsby redirects
  • Client-side paths
  • Running the server during the build, so that you can fetch the data from your API during build
  • Running and automatically restarting the server during development, so that you can develop your backend along with frontend
  • Serving the site with pathPrefix - set it up inside gatsby-config.js, the plugin will take care of it

Installation

Install the plugin using npm or yarn

npm install gatsby-plugin-nodejs

and add it to your gatsby-config.js

module.exports = {
  /* Site config */
  plugins: [
    /* Rest of the plugins */
    `gatsby-plugin-nodejs`,
  ],
};

Usage

Install the plugin as shown above, and create an index.js file inside server directory of your project

The server will run automatically during site build, and during development. In addition, when running in development mode (gatsby develop), it uses nodemon to automatically restart the server, when its' files change.

With Express

First install express as a dependency to your project - npm i express --save

Next, create a base of an Express server, and initialize Gatsby.

const express = require("express");
const gatsby = require("gatsby-plugin-nodejs");

const app = express();

gatsby.prepare({ app }, () => {
  // Here you can define your routes
});

const port = process.env.PORT || 1337;

app.listen(port, () => console.log(`listening on port ${port}`));

With Fastify

Install fastify 2.x and fastify-static as dependencies to your project - npm i fastify fastify-static --save

Next, create a basic Fastify server, and initialize Gatsby.

const fastify = require("fastify")();
const gatsby = require("gatsby-plugin-nodejs");

gatsby.prepare({ app: fastify, framework: "fastify" }, () => {});

const port = process.env.PORT || 1337;

fastify.listen(port, (err) => {
  if (err) {
    console.log(err);
    process.exit(1);
  }

  console.log(`listening on port ${port}`);
});

As you can see, you'll need to use prepare function. It creates a middleware, that serves static files, defines routes that handle Gatsby redirects and client paths, and sets up the 404 page.

Under the hood, your routes are added to server at the end, just before the 404 page route.

The prepare function accepts the config object as the first argument:

| Property | Value | Description | | ----------- | -------- | -------------------------------------------------------------------- | | app | object | App instance of your framework | | framework | string | If you use framework other than Express, pass a string with its name |

And a callback function as a second. It is executed just before 404 page route is defined, and should contain all your routes like API, etc.

When the server is set up, add an npm script:

{
  "_comment": "...rest of your package.json",
  "scripts": {
    "_comment": "...rest of your npm scripts",
    "start": "node server/index.js"
  }
}

Next build the page using gatsby build, and your server is ready to launch (npm start)

The server will launch during the build process, and when running gatsby develop. In addition, gatsby-plugin-nodejs uses nodemon, to automatically restart the server when its' files change.

Examples

  • Basic example with Express - https://github.com/AdamSiekierski/gatsby-plugin-nodejs-example-express/
  • Basic example with Fastify - https://github.com/AdamSiekierski/gatsby-plugin-nodejs-example-fastify/

Todo

  • [x] Integration with Express.js
  • [x] Support for creating pathPrefix
  • [x] Run server on build, so that Gatsby could be able to fetch data from it
  • [x] Run server along with development server
  • [x] Integration with Fastify
  • [ ] If the site isn't built when server is launched, build it automatically
  • [ ] Custom server filename and location

License

MIT © Adam Siekierski