New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

minimal-api

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

minimal-api

`Minimal-API` is a sleek and efficient Node.js package for creating HTTP servers. It's designed to be lightweight and dependency-free, offering a streamlined approach to server setup and route management. The package provides `createServer` and `getServer

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-62.5%
Maintainers
1
Weekly downloads
 
Created
Source

Minimal-API

Minimal-API is a sleek and efficient Node.js package for creating HTTP servers. It's designed to be lightweight and dependency-free, offering a streamlined approach to server setup and route management. The package provides createServer and getServer functions for a seamless development experience.

Features

  • Zero Dependencies: Fully functional with no third-party dependencies.
  • Quick Server Setup: Effortlessly set up an HTTP server with customizable options.
  • CORS Support: In-built CORS handling for cross-origin requests.
  • Dynamic Route Handling: Add and manage routes easily.
  • Singleton Server Access: Conveniently access your server instance across your application.

Installation

Install the package via npm:

npm install minimal-api

Usage

Creating a Server

Initialize your server using createServer with ES6 syntax:

import { createServer } from "minimal-api";

const port = 4000;

const server = createServer({ port, cors: true }, () => {
  console.log(`Server listening on port ${port}`);
});
Options
OptionTypeDescriptionDefault
portnumberServer listening port.4000
corsboolean | ObjectConfigures CORS. Set to true for default settings, or provide an object for detailed configuration.false
serverOptionshttp.ServerOptionsAdditional options for the HTTP server.{}

CORS Object Structure:

PropertyTypeDescription
originstring | string[]Specify allowed origins.
methodsstring | string[]Allowed HTTP methods.
allowedHeadersstring | string[]Custom headers that the server will accept.
exposedHeadersstring | string[]Headers that are safe to expose to the client.
credentialsbooleanIndicates whether the request can include user credentials.
maxAgenumberMaximum age for the CORS settings.

MinimalRequest Type:

PropertyTypeDescription
urlstring | undefinedThe request URL.
methodMethod | undefinedThe HTTP method used.
headershttp.IncomingHttpHeadersThe request headers.
searchParamsURLSearchParamsThe URL search parameters.
paramsRecord<string, string | undefined>URL parameters.
json() => Promise<T>Function to get JSON body.
text() => Promise<string>Function to get text body.
arrayBuffer() => Promise<ArrayBuffer>Function to get ArrayBuffer body.

Handler Type:

PropertyTypeDescription
requestMinimalRequestThe request object.
ReturnsPromise< { contentType?: string; statusCode?: number; data?: any; headers?: Record<string, string>; } | undefined | void >The response object or void.

Adding Routes

Use addRoute to define routes on your server with ES6 syntax:

import { MinimalRequest, Handler } from "minimal-api";

const routeHandler: Handler = async (request: MinimalRequest) => {
  // Handle the request
  // Return response details
};

server.addRoute("get", "/", routeHandler);

Accessing the Server Instance

Retrieve the server instance anywhere in your code using ES6 imports:

import { getServer } from "minimal-api";

const server = getServer();
// Now use 'server' as needed

Example

Complete example of using minimal-api to set up a server, add routes, and access the server instance using ES6:

import { createServer, getServer, MinimalRequest, Handler } from "minimal-api";

// Server setup
const server = createServer({ port: 4000, cors: true }, () => {
  console.log("Server is running on port 4000");
});

// Adding a route
const helloWorldHandler: Handler = async (request: MinimalRequest) => {
  // Handle request and return response details
  return {
    contentType: "text/plain",
    statusCode: 200,
    data: "Hello from Minimal-API!",
  };
};

server.addRoute("get", "/", helloWorldHandler);

// Accessing the server instance
const serverInstance = getServer();
// Utilize 'serverInstance' as required

Contributing

Contributions are what make the open-source community an amazing place to learn, inspire, and create. Any contributions you make are immensely appreciated.

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Saiban Ali - @saiban-ali

Project Link: https://github.com/saiban-ali/minimal-api

Keywords

FAQs

Package last updated on 28 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc