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.
Install Sapix via npm:
npm install sapix
Below is an example of setting up a server using Sapix and configuring multiple routes with different HTTP methods.
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!
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" } }
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." }
routes.delete('/profile', (res) => {
res.status(200).sendJSON({ message: 'Profile deleted successfully.' });
});
- Purpose: Responds with a message confirming deletion.
- Response:
{ "message": "Profile deleted successfully." }
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." }
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>
routes.get('/error', (res) => {
res.sendError('An unexpected error occurred.', 500);
});
- Purpose: Simulates a server error response.
- Response:
{ "error": "An unexpected error occurred." }
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
.
- 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.
├── index.js // Main server script
└── package.json // Project metadata and dependencies
-
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.
Use sendError
to return error responses:
routes.get('/error', (res) => {
res.sendError('Something went wrong!', 500);
});
Feel free to contribute to this project by submitting pull requests or opening issues.
This project is licensed under the MIT License.
Enjoy the Simple API Processing and Integration eXperience with Sapix! 🚀