nite-server-side
v1.1.0
Published
Scalable Server side framework for NITE
Readme
Absolutely! Here's a complete, ready-to-use README.md for NITE Server-Side. You can copy it directly into your project.
# NITE Server-Side
**NITE Server-Side** is a scalable, TypeScript-first server-side framework designed to simplify API creation with decorators, action classes, and automatic JSON serialization.
It enforces best practices by requiring that action methods are explicitly serialized before returning data.
---
## Features
- ⚡ **Decorator-based routing** (`@Get`, `@Post`)
- 🛡️ **Automatic serialization enforcement** via `@Serialize`
- 🧩 **Separation of concerns**: Controllers delegate logic to Action classes
- 🌐 **CORS support**
- 🔥 **TypeScript-first**, fully typed responses
- 🚨 **Startup validation** to ensure all action methods are properly decorated
---
## Installation
```bash
npx nite-server-sideTypeScript is required:
npm install typescript @types/node --save-devProject Structure
src/
├─ controllers/
│ └─ UserController.ts
├─ actions/
│ └─ UserAction.ts
├─ types/
│ └─ userType.ts
---
## Getting Started
### **1. Define Action Classes**
Action classes contain the business logic. Every method **must** be decorated with `@Serialize`:
```ts
import { Serialize } from '../../library/modules/decorators/api-route-names';
export class UserAction {
@Serialize
async sayHelloWorld() {
return { success: true, message: 'Hello world', data: null };
}
@Serialize
async createUser(email: string, password: string) {
return { success: true, message: 'User created', data: { token: 'abc123' } };
}
}2. Define Controllers
Controllers define the API routes and delegate logic to actions:
import { Get, Post, ParseBody } from '../../library/modules/decorators/api-route-names';
import { UserAction } from '../actions/UserAction';
import type { CreateUserType } from '../types/userType';
export class UserController {
constructor(private readonly userAction: UserAction = new UserAction()) {}
@Get('/api/ping-server')
async pingServer() {
return await this.userAction.sayHelloWorld();
}
@Post('/api/create-user')
@ParseBody
async createUser(body: CreateUserType) {
return await this.userAction.createUser(body.email, body.password);
}
@Get('/')
async greet() {
return { message: 'Hello world' };
}
}3. Setup the Server
Register controllers and actions with APIHandler, validate serialization, then start the server:
import { ServerHandler } from 'nite-server-side';
import { UserController } from './controllers/UserController';
import { UserAction } from './actions/UserAction';
ServerHandler.registerControllers([UserController]);
ServerHandler.registerActions([UserAction]);
// Validate all registered action methods have @Serialize
ServerHandler.validateSerializedActions();
ServerHandler.listen(3000, { cors: true });4. Making Requests
Ping the server
const pingServer = async () => {
const res = await fetch('http://localhost:3000/api/ping-server');
const json = await res.json();
console.log(json); // { success: true, message: 'Hello world', data: null }
};Create a user
const createUser = async () => {
const res = await fetch('http://localhost:3000/api/create-user', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email: '[email protected]', password: '123456' })
});
const json = await res.json();
console.log(json);
};Decorators
| Decorator | Usage | Description |
| ------------- | ---------------------- | --------------------------------------------------------------------------------- |
| @Get(path) | @Get('/api/ping') | Maps a method to a GET route |
| @Post(path) | @Post('/api/create') | Maps a method to a POST route |
| @ParseBody | @ParseBody | Automatically parses JSON request body |
| @Serialize | @Serialize | Ensures method output is serialized to JSON; mandatory for registered actions |
| @Required | @Required | Ensures controller/action registration |
Much More.....
Best Practices
- Always decorate action methods with
@Serialize. - Keep controllers thin; delegate business logic to actions.
- Register all actions using
registerActions()to enable serialization validation. - Use
validateSerializedActions()beforelisten()to catch missing decorators at startup.
Error Handling
If an action method is missing @Serialize, the server will throw immediately:
🚨 Action method "sayHelloWorld" in "UserAction" must have @Serialize decorator!This prevents runtime errors and ensures consistent API responses.
License
ISC – Nicholas Johnson
Contributing
- Fork the repository
- Make your changes in a feature branch
- Open a pull request
Summary
NITE Server-Side is designed for:
- Type-safe, decorator-based API development
- Strict enforcement of JSON serialization in action methods
- Clean separation of controllers and actions
It’s perfect for scalable Node.js projects using TypeScript, enforcing consistency and best practices out of the box.
