Fastify (Masqr)

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

Prerequisites

  • Node.js v18 or later
  • A Masqr server.
  • 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 masqr 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
masqrUrlstringThe Url of the Masqr server.Yes
builtinCookieParserbooleanWhether to use the built-in cookie parser.No

Examples

  • Minimal example of using Corlink (Masqr compat) 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 { masqr } = require('@rubynetwork/corlink-fastify');

const app = fastify();
app.register(masqr, {
    deniedFilePath: 'denied.html',
    v3: false, //set to true if you want to use multiple .html files (based off of the hostname)
    unlockedPaths: ['/bare/'],
    whiteListedURLs: ['https://maindomain.com', 'https://subdomain.maindomain.com'],
    masqrUrl: 'https://corlink.example.com/validate?license=',
    //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 { masqr } from '@rubynetwork/corlink-fastify';

const app = fastify();

app.register(masqr, {
    deniedFilePath: 'denied.html',
    unlockedPaths: ['/bare/'],
    whiteListedURLs: ['https://maindomain.com', 'https://subdomain.maindomain.com'],
    masqrUrl: 'https://corlink.example.com/validate?license=',
    //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 { masqr } = require('@rubynetwork/corlink-fastify');
const fastifyCookie = require('@fastify/cookie');

const app = fastify();
app.register(fastifyCookie, {
    secret: 'your-secret',
    parseOptions: {},
});
app.register(masqr, {
    deniedFilePath: 'denied.html',
    unlockedPaths: ['/bare/'],
    whiteListedURLs: ['https://maindomain.com', 'https://subdomain.maindomain.com'],
    masqrUrl: 'https://corlink.example.com/validate?license=',
    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 { masqr } from '@rubynetwork/corlink-fastify';
import fastifyCookie from '@fastify/cookie';

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

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

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