Fastify

This guide will show you how to use Corlink with Fastify. And provide minimal examples of using Corlink with Fastify.

Prerequisites

  • Node.js v18 or later
  • An API key from a Corlink server. - Need One? Join our Discord and open a ticket.
  • A denied file. - Need help setting up a denied file? Check out our Denied File Setup guide

Installation

First, install the @rubynetwork/corlink-fastify package and fastify and (optionally) @fastify/cookie if you haven't already installed them in your project

NPM
npm install @rubynetwork/corlink-fastify fastify @fastify/cookie
Yarn
yarn add @rubynetwork/corlink-fastify fastify @fastify/cookie
PNPM
pnpm add @rubynetwork/corlink-fastify fastify @fastify/cookie

Options

  • Options for the corlinkExpress middleware.
OptionTypeDescriptionRequired
deniedFilePathstringThe path to the file that will be served when a request is denied.Yes
unlockedPathsstringAn array of paths that will not be checked by Corlink.Yes
whiteListedURLsstringAn array of URLs that will not be checked by Corlink.Yes
corlinkUrlstringThe Url of the Corlink server.Yes
corlinkAPIKeystringThe API key of the Corlink server.Yes
builtinCookieParserbooleanWhether to use the built-in cookie parser.No

Examples

  • Minimal example of using Corlink with Fastify (both CommonJS and ES Modules).
  • The examples with (cookie parse) are using the built-in cookie parser. If you want to use your own cookie parser, you can set builtinCookieParser to false and use your own cookie parser.
const express = require('fastify');
const corlink = require('@rubynetwork/corlink-fastify');

const app = fastify();
app.register(corlink, {
    deniedFilePath: 'denied.html',
    unlockedPaths: ['/bare/'],
    whiteListedURLs: ['https://maindomain.com', 'https://subdomain.maindomain.com'],
    corlinkUrl: 'https://corlink.example.com',
    corlinkAPIKey: 'your-api-key',
    //can be optionally deleted if you just want the default
    builtinCookieParser: true,
});

app.get('/', (req, res) => {
    res.send('Hello World!');
});

app.listen({ port: 3000 }, () => {
    console.log('Example app listening on port 3000!');
});
import fastify from 'fastify';
import corlink from '@rubynetwork/corlink-fastify';

const app = fastify();

app.register(corlink, {
    deniedFilePath: 'denied.html',
    unlockedPaths: ['/bare/'],
    whiteListedURLs: ['https://maindomain.com', 'https://subdomain.maindomain.com'],
    corlinkUrl: 'https://corlink.example.com',
    corlinkAPIKey: 'your-api-key',
    //can be optionally deleted if you just want the default
    builtinCookieParser: true,
});

app.get('/', (req, res) => {
    res.send('Hello World!');
});

app.listen({ port: 3000 }, () => {
    console.log('Example app listening on port 3000!');
});
const fastify = require('fastify');
const corlink = require('@rubynetwork/corlink-fastify');
const fastifyCookie = require('@fastify/cookie');

const app = fastify();
app.register(fastifyCookie, {
    secret: 'your-secret',
    parseOptions: {},
});
app.register(corlink, {
    deniedFilePath: 'denied.html',
    unlockedPaths: ['/bare/'],
    whiteListedURLs: ['https://maindomain.com', 'https://subdomain.maindomain.com'],
    corlinkUrl: 'https://corlink.example.com',
    corlinkAPIKey: 'your-api-key',
    builtinCookieParser: false,
});

app.get('/', (req, res) => {
    res.send('Hello World!');
});

app.listen({ port: 3000 }, () => {
    console.log('Example app listening on port 3000!');
});
import fastify from 'fastify';
import corlink from '@rubynetwork/corlink-fastify';
import fastifyCookie from '@fastify/cookie';

const app = fastify();
app.register(fastifyCookie, {
    secret: 'your-secret',
    parseOptions: {},
});
app.register(corlink, {
    deniedFilePath: 'denied.html',
    unlockedPaths: ['/bare/'],
    whiteListedURLs: ['https://maindomain.com', 'https://subdomain.maindomain.com'],
    corlinkUrl: 'https://corlink.example.com',
    corlinkAPIKey: 'your-api-key',
    builtinCookieParser: false,
});

app.get('/', (req, res) => {
    res.send('Hello World!');
});

app.listen({ port: 3000 }, () => {
    console.log('Example app listening on port 3000!');
});