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

Home

Simple API Router Wiki

Welcome to the Simple API Router Wiki! This documentation provides an in-depth guide to installing, using, and extending the package.

Table of Contents


Installation

Prerequisites

Ensure you have Node.js and npm installed:

node -v
npm -v

Installing the Package

To install the package, run:

npm install @notnexx/n-sar

Usage

1. Setting Up an Express Server

Create 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}`);
});

2. Automatically Generate a Sample Project

To create a sample project with predefined structure:

npx n-sar my-api-project

This creates a folder with:

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

Start the project:

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 changes (git commit -m 'Added feature xyz').
  4. Push (git push origin feature-xyz).
  5. Create a Pull Request.

License

This project is licensed under the MIT License.

Clone this wiki locally