API Reference
Core API
Section titled “Core API”NexaSpace
Section titled “NexaSpace”The main application class.
Constructor
Section titled “Constructor”new NexaSpace(options?: NexaSpaceOptions)Options:
| Parameter | Type | Default | Description |
|---|---|---|---|
production | boolean | false | Enable production mode |
port | number | 3000 | Server port |
host | string | 'localhost' | Server host |
cache | CacheOptions | {} | Cache configuration |
logging | LogOptions | {} | Logging configuration |
Example:
const app = new NexaSpace({ production: true, port: 8080, cache: { enabled: true }});HTTP Methods
Section titled “HTTP Methods”app.get()
Section titled “app.get()”Register a GET route handler.
app.get(path: string, ...handlers: Handler[]): voidParameters:
path- Route path (supports parameters like/users/:id)handlers- One or more request handlers
Example:
app.get('/users/:id', async (req, res) => { const user = await db.users.findById(req.params.id); res.json(user);});app.post()
Section titled “app.post()”Register a POST route handler.
app.post(path: string, ...handlers: Handler[]): voidapp.put()
Section titled “app.put()”Register a PUT route handler.
app.put(path: string, ...handlers: Handler[]): voidapp.delete()
Section titled “app.delete()”Register a DELETE route handler.
app.delete(path: string, ...handlers: Handler[]): voidMiddleware
Section titled “Middleware”app.use()
Section titled “app.use()”Register middleware or plugin.
app.use(middleware: Middleware | Plugin): voidExample:
// Custom middlewareapp.use((req, res, next) => { console.log(`${req.method} ${req.path}`); next();});
// Pluginimport { CORS } from 'nexaspace';app.use(CORS());Request Object
Section titled “Request Object”The request object represents the HTTP request.
Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
body | any | Parsed request body |
params | Record<string, string> | URL parameters |
query | Record<string, string> | Query string parameters |
headers | Record<string, string> | Request headers |
method | string | HTTP method |
path | string | Request path |
ip | string | Client IP address |
user | User | null | Authenticated user (if auth middleware is used) |
Methods
Section titled “Methods”req.get()
Section titled “req.get()”Get request header value.
req.get(name: string): string | undefinedExample:
const contentType = req.get('content-type');const auth = req.get('authorization');Response Object
Section titled “Response Object”The response object represents the HTTP response.
Methods
Section titled “Methods”res.json()
Section titled “res.json()”Send JSON response.
res.json(data: any, status?: number): voidExample:
res.json({ success: true, data: users });res.json({ error: 'Not found' }, 404);res.send()
Section titled “res.send()”Send text response.
res.send(data: string, status?: number): voidres.status()
Section titled “res.status()”Set response status code.
res.status(code: number): ResponseExample:
res.status(201).json({ created: true });res.redirect()
Section titled “res.redirect()”Redirect to another URL.
res.redirect(url: string, status?: number): voidAuthentication
Section titled “Authentication”JWTAuth
Section titled “JWTAuth”JWT authentication middleware.
JWTAuth()
Section titled “JWTAuth()”Configure JWT authentication.
JWTAuth(options: JWTOptions): MiddlewareOptions:
| Parameter | Type | Required | Description |
|---|---|---|---|
secret | string | Yes | JWT secret key |
expiresIn | string | No | Token expiration (e.g., ‘7d’) |
algorithms | string[] | No | Allowed algorithms |
JWTAuth.sign()
Section titled “JWTAuth.sign()”Generate JWT token.
JWTAuth.sign(payload: object, expiresIn?: string): stringExample:
const token = JWTAuth.sign( { userId: user.id, role: user.role }, '24h');JWTAuth.require()
Section titled “JWTAuth.require()”Middleware to require authentication.
JWTAuth.require(): MiddlewareDatabase
Section titled “Database”Database Connection
Section titled “Database Connection”import { Database } from 'nexaspace';
const db = new Database({ type: 'postgresql', host: 'localhost', port: 5432, database: 'nexaspace', username: 'user', password: 'pass'});Query Methods
Section titled “Query Methods”db.query()
Section titled “db.query()”Execute raw SQL query.
db.query<T>(sql: string, params?: any[]): Promise<T[]>db.insert()
Section titled “db.insert()”Insert record.
db.insert(table: string, data: object): Promise<number>db.update()
Section titled “db.update()”Update records.
db.update(table: string, data: object, where: object): Promise<number>db.delete()
Section titled “db.delete()”Delete records.
db.delete(table: string, where: object): Promise<number>Cache Configuration
Section titled “Cache Configuration”import { Cache } from 'nexaspace';
const cache = new Cache({ type: 'redis', host: 'localhost', port: 6379, ttl: 3600 // Default TTL in seconds});Methods
Section titled “Methods”cache.get()
Section titled “cache.get()”Get cached value.
cache.get<T>(key: string): Promise<T | null>cache.set()
Section titled “cache.set()”Set cached value.
cache.set(key: string, value: any, ttl?: number): Promise<void>cache.del()
Section titled “cache.del()”Delete cached value.
cache.del(key: string): Promise<void>cache.clear()
Section titled “cache.clear()”Clear all cached values.
cache.clear(): Promise<void>Type Definitions
Section titled “Type Definitions”Handler
Section titled “Handler”Request handler function type.
type Handler = ( req: Request, res: Response, next?: NextFunction) => void | Promise<void>Middleware
Section titled “Middleware”Middleware function type.
type Middleware = ( req: Request, res: Response, next: NextFunction) => void | Promise<void>NextFunction
Section titled “NextFunction”Next middleware function type.
type NextFunction = (error?: Error) => void