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 🙏

© 2025 – Pkg Stats / Ryan Hefner

selaras-api

v1.0.5

Published

## Overview

Downloads

57

Readme

Selaras API Documentation

Overview

The selaras-api library provides a lightweight and secure server framework for building RESTful APIs. It simplifies creating a secure server with pre-configured middleware, route handling, and essential security headers.

This documentation covers the setup and usage of the selaras-api library for creating a secure server application.


Installation

Install the selaras-api package using npm:

npm install selaras-api

Features

  • Secure by Default: Implements essential security headers (e.g., CSP, HSTS, X-Frame-Options).
  • Customizable Middleware: Easily add or configure middleware for request handling.
  • RESTful API Support: Register routes for standard HTTP methods.
  • Built-in Logging: Logs incoming requests for debugging or analytics.

Example Usage

The following code demonstrates how to use the selaras-api library:

import {
  SelarasServer,
  SelarasResponse,
  SelarasRequest,
  logger,
} from "selaras-api";

// Create an instance of SelarasServer
const app = new SelarasServer();

// Use built-in logger middleware
app.use(logger);

// Set security headers
app.securityHeaders({
  ContentSecurityPolicy:
    "default-src 'self'; script-src 'self'; style-src 'self';",
  StrictTransportSecurity: "max-age=63072000; includeSubDomains; preload",
  XContentTypeOptions: "nosniff",
  XFrameOptions: "DENY",
  ReferrerPolicy: "strict-origin-when-cross-origin",
  PermissionsPolicy: "geolocation=(self), microphone=(self)",
});

// Middleware for logging requests
app.use((req: SelarasRequest, res: SelarasResponse, next: Function) => {
  console.log(`[${req.method}] ${req.url}`);
  next();
});

// Define RESTful routes
app.route("GET", "/", (req: SelarasRequest, res: SelarasResponse) => {
  app.send(res, 200, "Welcome to the secure SelarasServer!");
});

app.route("POST", "/data", (req: SelarasRequest, res: SelarasResponse) => {
  app.send(res, 201, "POST Request - Data Created");
});

app.route("PUT", "/data", (req: SelarasRequest, res: SelarasResponse) => {
  app.send(res, 200, "PUT Request - Data Updated");
});

app.route("DELETE", "/data", (req: SelarasRequest, res: SelarasResponse) => {
  app.send(res, 200, "DELETE Request - Data Deleted");
});

app.route("PATCH", "/data", (req: SelarasRequest, res: SelarasResponse) => {
  app.send(res, 200, "PATCH Request - Data Partially Updated");
});

// Start the server on port 3008
app.start(3008);

Security Headers

The selaras-api library applies the following security headers by default:

  • Content-Security-Policy: Restricts loading resources from specific origins.
  • Strict-Transport-Security: Enforces HTTPS connections with a max-age policy.
  • X-Content-Type-Options: Prevents MIME sniffing.
  • X-Frame-Options: Protects against clickjacking by disallowing iframe embedding.
  • Referrer-Policy: Controls how much referrer information is shared.
  • Permissions-Policy: Restricts access to sensitive features like geolocation.

Middleware

Middleware functions are used to process requests before sending responses. For example:

app.use((req: SelarasRequest, res: SelarasResponse, next: Function) => {
  console.log(`[Middleware] Processing request for ${req.url}`);
  next();
});

API Methods

app.use(middleware: Function)

Adds a middleware to handle requests.

app.securityHeaders(headers: Record<string, string>)

Configures security headers for all responses.

app.route(method: string, path: string, handler: Function)

Registers a route for a specific HTTP method and path.

app.send(response: SelarasResponse, statusCode: number, message: string)

Sends a response with the specified status code and message.

app.start(port: number)

Starts the server on the specified port.


About the Author

Muhammad Imam Rozali
Founder of Selaras Technology
GitHub: https://github.com/imamrozali


Contributing

Feel free to open issues or submit pull requests to improve selaras-api. Contributions are always welcome!


License

This library is licensed under the MIT License. See the LICENSE file for details.


For more information, refer to the npm package page.