1.0.1 β€’ Published 8 months ago

nanolet v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Nanolet

Nanolet is a lightweight and minimal Node.js framework designed to simplify the creation of HTTP servers. It provides an intuitive API for routing, middleware management, and response handling, making it an ideal choice for developers who want to build web servers with ease.


Features

  • πŸŒ€ Minimal and Lightweight: Focused on simplicity and efficiency.
  • πŸ”— Middleware Support: Chain middleware functions for cleaner and modular code.
  • πŸš€ Customizable Routing: Easily define routes for your application.
  • πŸ“‚ File Streaming: Send files with ease using the built-in res.sendFile.
  • 🌐 JSON Responses: Send JSON responses with a single method.
  • πŸ“¦ Plug-and-Play: Start using Nanolet with minimal setup.

Installation

To install Nanolet, run:

npm install nanolet

Getting Started

Here’s how to get started with Nanolet:

Basic Usage

const Nanolet = require("nanolet");

const server = new Nanolet();

server.route("get", "/", (req, res) => {
  res.json({ message: "Welcome to Nanolet!" });
});

server.route("get", "/hello", (req, res) => {
  res.status(200).json({ greeting: "Hello, World!" });
});

server.listen(3000, () => {
  console.log("Nanolet server running on http://localhost:3000");
});

Middleware

You can use beforeEach to add middleware that runs before your routes:

server.beforeEach((req, res, next) => {
  console.log(`${req.method} ${req.url}`);
  next();
});

File Streaming

Send a file to the client using res.sendFile:

server.route("get", "/file", (req, res) => {
  res.sendFile("./path/to/file.txt", "text/plain");
});

API Reference

Nanolet.route(method, path, callback)

  • Define a route with a specific HTTP method and path.
  • Parameters:
    • method (string): The HTTP method (e.g., "get", "post").
    • path (string): The route path (e.g., "/", "/api").
    • callback (function): The function to handle the route, with (req, res).

Nanolet.beforeEach(callback)

  • Add middleware to execute before all routes.
  • Parameters:
    • callback (function): The middleware function, with (req, res, next).

res.sendFile(path, mimeType)

  • Send a file to the client.
  • Parameters:
    • path (string): Path to the file.
    • mimeType (string): MIME type of the file (e.g., "text/plain").

res.status(code)

  • Set the HTTP status code for the response.
  • Parameters:
    • code (number): The HTTP status code.

res.json(data)

  • Send a JSON response.
  • Parameters:
    • data (object): The JSON data to send.

Example Application

const Nanolet = require("nanolet");

const server = new Nanolet();

server.beforeEach((req, res, next) => {
  console.log(`Request received: ${req.method} ${req.url}`);
  next();
});

server.route("get", "/", (req, res) => {
  res.json({ message: "Hello, Nanolet!" });
});

server.route("post", "/data", (req, res) => {
  res.status(201).json({ success: true, data: "Your data has been saved!" });
});

server.route("get", "/file", (req, res) => {
  res.sendFile("./example.txt", "text/plain");
});

server.listen(3000, () => {
  console.log("Server running at http://localhost:3000");
});

Contributing

Contributions are welcome! If you’d like to contribute: 1. Fork the repository. 2. Create a new branch for your feature or bug fix. 3. Submit a pull request with a detailed description of your changes.


License

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


Author

Created with ❀️ by Akhil V.
Feel free to reach out on GitHub for any questions or feedback.

1.0.1

8 months ago

1.0.0

8 months ago