Skip to content
NotNexx edited this page Feb 24, 2025 · 3 revisions

Simple API Router Wiki

Welcome to the Simple API Router Wiki!

This documentation provides a comprehensive guide to installing, using, and extending the package efficiently.


πŸ“– Table of Contents


πŸš€ Installation

Prerequisites

Before installing, ensure you have Node.js and npm installed:

node -v
npm -v

Installing the Package

To install the package, run the following command:

npm install @notnexx/n-sar

⚑ Usage

Setting Up an Express Server

To quickly set up an Express server and use n-sar to load routes from the api/ folder:

const express = require('express');
const loadRoutes = require('@notnexx/n-sar');

const app = express();
const PORT = 3000;

app.use(express.json());
loadRoutes(app);

app.listen(PORT, () => {
    console.log(`πŸš€ Server running at http://localhost:${PORT}`);
});

Automatically Generate a Sample Project

To create a sample project with a predefined structure:

npx n-sar my-api-project

This creates a folder structure like:

my-api-project/
β”œβ”€β”€ api/
β”‚   β”œβ”€β”€ router.js
β”‚   └── user.js
β”œβ”€β”€ package.json
└── server.js

Start the project by running:

cd my-api-project
npm install
node server.js

πŸ“œ API Documentation

Folder Structure

Each file in api/ represents an endpoint. Example:

api/user.js

const express = require('express');
const router = express.Router();

router.get('/', (req, res) => {
    res.json({ message: 'User endpoint' });
});

module.exports = router;

This automatically registers the route:

GET /user

πŸ”§ Generating API Clients

To generate a frontend API client that connects to your backend:

npx n-sar export-routes http://localhost:3000

This creates apiClient.js:

import APIClient from './apiClient';

APIClient.user().then(response => console.log(response));

🀝 Contributing

How to Contribute

  1. Fork the repository.
  2. Create a new branch:
    git checkout -b feature-xyz
  3. Commit your changes:
    git commit -m 'Added feature xyz'
  4. Push to GitHub:
    git push origin feature-xyz
  5. Create a Pull Request and describe your changes.

πŸ“œ License

This project is licensed under the MIT License.