You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Welcome toSapix, a lightweight library designed to help you build robust servers with intuitive routing. This documentation provides examples and instructions to get you started usingSapix effectively.
Installation
InstallSapix via npm:
npm install sapix
Usage Overview
Below is an example of setting up a server usingSapix 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 toPOST requests with a confirmation message.
Once your routes are defined, start the server using the following code:
import{sapixServer,sapixRoutes}from'sapix';// Create routesconstroutes=newsapixRoutes();// Start the serverconstserver=newsapixServer().setPort(4000)// Specify the port.setRoute(routes)// Attach the routes.start();// Start the server
Your server will now be running athttp://localhost:4000.
Key Features of Sapix
Simple: Easy to configure with minimal setup.
API-oriented: Focused on building APIs efficiently.
Processing-friendly: Handles different HTTP methods (GET, POST, PATCH, DELETE).
Integrated: Supports dynamic routes, query params, and path params.
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: Visithttp://localhost:4000/profile/1/user/456 in your browser or Postman.
POST Request: Send aPOST request tohttp://localhost:4000/profile with an appropriate payload.
Error Handling
UsesendError 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 theMIT License.
Enjoy theSimple API Processing and Integration eXperience with Sapix! 🚀