handdeng-node-server
TypeScript icon, indicating that this package has built-in type declarations

1.2.0-beta.1 • Public • Published

handdeng-node-server

Introduction

handdeng-node-server is a lightweight HTTP server framework built on Node.js, designed to simplify the creation and management of API services. This framework provides flexible routing, permission verification, parameter validation, and error handling mechanisms, making it ideal for building high-performance RESTful API services.

Features

  • GET/POST Routing: Supports the registration of handler functions for GET and POST requests.
  • Permission Verification: Allows enabling or disabling permission checks for individual APIs.
  • Parameter Validation: Supports type and required field validation for API request parameters.
  • Global Error Handling: Provides customizable global error handling mechanisms.
  • Flexible Routing: Supports custom handling for missing routes and incorrect HTTP methods.

Quick Start

  1. Install Dependencies

    npm install handdeng-node-server
  2. Create a Server Instance

    import { NodeServer } from "handdeng-node-server";
    const server = new NodeServer(3000, "localhost");
  3. Register API Routes

    server.get("/api/test", (req, res) => {
      res.writeHead(200, { "Content-Type": "application/json" });
      res.end(JSON.stringify({ message: "GET request success" }));
    });
    
    server.post("/api/test", (req, res) => {
      res.writeHead(200, { "Content-Type": "application/json" });
      res.end(JSON.stringify({ message: "POST request success" }));
    });
  4. Start the Server

    Run your script to start the server:

    node index.js
  5. Test the API

    You can test your API by visiting http://localhost:3000/api/test.

API Configuration Options

  • openPermissionVerify: Whether to enable permission verification. Default is true.
  • paramsList: A list of parameter validation rules, including the parameter key, type, and whether it's required.

Error Handling

  • catch: Global error handler function.
  • paramsError: Handler function for request parameter errors.
  • notFount: Handler function for cases where the route is not found.
  • methodsError: Handler function for incorrect HTTP methods.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.2.0-beta.19latest

Version History

VersionDownloads (Last 7 Days)Published
1.2.0-beta.19
1.1.87
1.1.74
1.1.64
1.1.55
1.1.42
1.1.30
1.1.20
1.1.10
1.1.00
1.0.90
1.0.80
1.0.70
1.0.60
1.0.50
1.0.40
1.0.30
1.0.20
1.0.10
1.0.00

Package Sidebar

Install

npm i handdeng-node-server

Weekly Downloads

31

Version

1.2.0-beta.1

License

MIT

Unpacked Size

31.6 kB

Total Files

15

Last publish

Collaborators

  • handdeng