Skip to content

karandaid/sapix

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sapix Library Documentation

SAPIX: Simple API Processing and Integration eXperience

Welcome to Sapix, a lightweight library designed to help you build robust servers with intuitive routing. This documentation provides examples and instructions to get you started using Sapix effectively.


Installation

Install Sapix via npm:

npm install sapix

Usage Overview

Below is an example of setting up a server using Sapix and configuring multiple routes with different HTTP methods.


Step-by-Step: Create Routes with Sapix

GET / – Home Route

routes.get('/', (res) => {
    res.status(200).sendText('Welcome to sapixRoutes Home Page!');
});
  • Purpose: Handles the homepage request and responds with text.
  • Response:
    Welcome to sapixRoutes Home Page!
    

GET /profile/:id/user/:userId – Dynamic Route with Params

routes.get('/profile/:id/user/:userId', (res, query, path_params) => {
    res.status(200).sendJSON({ 
        message: 'This is a JSON response.', 
        query, 
        path_params 
    });
});
  • Purpose: Responds with JSON including query and path parameters.
  • Example Request:
    GET /profile/1/user/456?status=active
    
  • Example Response:
    {
      "message": "This is a JSON response.",
      "query": { "status": "active" },
      "path_params": { "id": "1", "userId": "456" }
    }

HTTP Method Examples

POST /profile – Create User Profile

routes.post('/profile', (res) => {
    res.status(200).sendJSON({ message: 'Profile created successfully.' });
});
  • Purpose: Responds to POST requests with a confirmation message.
  • Response:
    { "message": "Profile created successfully." }

DELETE /profile – Delete User Profile

routes.delete('/profile', (res) => {
    res.status(200).sendJSON({ message: 'Profile deleted successfully.' });
});
  • Purpose: Responds with a message confirming deletion.
  • Response:
    { "message": "Profile deleted successfully." }

PATCH /profile – Update User Profile

routes.patch('/profile', (res) => {
    res.status(200).sendJSON({ message: 'Profile updated successfully.' });
});
  • Purpose: Responds with a message confirming the profile update.
  • Response:
    { "message": "Profile updated successfully." }

Additional Routes

GET /about – About Page

routes.get('/about', (res) => {
    res.status(200).sendHTML('<h1>Welcome to the About Page!</h1>');
});
  • Purpose: Sends an HTML response for the "About" page.
  • Response:
    <h1>Welcome to the About Page!</h1>

GET /error – Error Handling

routes.get('/error', (res) => {
    res.sendError('An unexpected error occurred.', 500);
});
  • Purpose: Simulates a server error response.
  • Response:
    { "error": "An unexpected error occurred." }

Starting the Sapix Server

Once your routes are defined, start the server using the following code:

import { sapixServer, sapixRoutes } from 'sapix';

// Create routes
const routes = new sapixRoutes();

// Start the server
const server = new sapixServer()
    .setPort(4000)    // Specify the port
    .setRoute(routes) // Attach the routes
    .start();         // Start the server

Your server will now be running at http://localhost:4000.


Key Features of Sapix

  1. Simple: Easy to configure with minimal setup.
  2. API-oriented: Focused on building APIs efficiently.
  3. Processing-friendly: Handles different HTTP methods (GET, POST, PATCH, DELETE).
  4. Integrated: Supports dynamic routes, query params, and path params.
  5. Xperience-first: Provides error handling and a smooth developer experience.

Project Structure

├── index.js           // Main server script
└── package.json       // Project metadata and dependencies

How to Test

  • GET Request:
    Visit http://localhost:4000/profile/1/user/456 in your browser or Postman.

  • POST Request:
    Send a POST request to http://localhost:4000/profile with an appropriate payload.


Error Handling

Use sendError to return error responses:

routes.get('/error', (res) => {
    res.sendError('Something went wrong!', 500);
});

Feedback & Contribution

Feel free to contribute to this project by submitting pull requests or opening issues.


License

This project is licensed under the MIT License.


Enjoy the Simple API Processing and Integration eXperience with Sapix! 🚀

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published