JSPM

  • Created
  • Published
  • Downloads 6136
  • Score
    100M100P100Q122913F
  • License Apache-2.0

Arcjet SDK for Node.js

Package Exports

  • @arcjet/node
  • @arcjet/node/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@arcjet/node) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Arcjet Logo

@arcjet/node

npm badge

Arcjet helps developers protect their apps in just a few lines of code. Implement rate limiting, bot protection, email verification, and defense against common attacks.

This is the Arcjet SDK for Node.js.

Looking for our Next.js framework SDK? Check out the @arcjet/next package.

Getting started

Visit the quick start guide to get started.

Example app

Try an Arcjet protected app live at https://example.arcjet.com (source code).

What is this?

This is our adapter to integrate Arcjet into Node.js. Arcjet helps you secure your Node server. This package exists so that we can provide the best possible experience to Node users.

When should I use this?

You can use this if you are using Node.js. See our Get started guide for other supported frameworks and runtimes.

Install

This package is ESM only. Install with npm in Node.js:

npm install @arcjet/node

Use

import http from "node:http";
import arcjet, { shield } from "@arcjet/node";

// Get your Arcjet key at <https://app.arcjet.com>.
// Set it as an environment variable instead of hard coding it.
const arcjetKey = process.env.ARCJET_KEY;

if (!arcjetKey) {
  throw new Error("Cannot find `ARCJET_KEY` environment variable");
}

const aj = arcjet({
  key: arcjetKey,
  rules: [
    // Shield protects your app from common attacks.
    // Use `DRY_RUN` instead of `LIVE` to only log.
    shield({ mode: "LIVE" }),
  ],
});

const server = http.createServer(async function (
  request: http.IncomingMessage,
  response: http.ServerResponse,
) {
  const decision = await aj.protect(request);

  if (decision.isDenied()) {
    response.writeHead(403, { "Content-Type": "application/json" });
    response.end(JSON.stringify({ message: "Forbidden" }));
    return;
  }

  response.writeHead(200, { "Content-Type": "application/json" });
  response.end(JSON.stringify({ message: "Hello world" }));
});

server.listen(8000);

For more on how to configure Arcjet with Node.js and how to protect Node, see the Arcjet Node.js SDK reference on our website.

License

Apache License, Version 2.0 © Arcjet Labs, Inc.