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.