Skip to content

API Reference

The main application class.

new NexaSpace(options?: NexaSpaceOptions)

Options:

ParameterTypeDefaultDescription
productionbooleanfalseEnable production mode
portnumber3000Server port
hoststring'localhost'Server host
cacheCacheOptions{}Cache configuration
loggingLogOptions{}Logging configuration

Example:

const app = new NexaSpace({
production: true,
port: 8080,
cache: { enabled: true }
});

Register a GET route handler.

app.get(path: string, ...handlers: Handler[]): void

Parameters:

  • 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);
});

Register a POST route handler.

app.post(path: string, ...handlers: Handler[]): void

Register a PUT route handler.

app.put(path: string, ...handlers: Handler[]): void

Register a DELETE route handler.

app.delete(path: string, ...handlers: Handler[]): void

Register middleware or plugin.

app.use(middleware: Middleware | Plugin): void

Example:

// Custom middleware
app.use((req, res, next) => {
console.log(`${req.method} ${req.path}`);
next();
});
// Plugin
import { CORS } from 'nexaspace';
app.use(CORS());

The request object represents the HTTP request.

PropertyTypeDescription
bodyanyParsed request body
paramsRecord<string, string>URL parameters
queryRecord<string, string>Query string parameters
headersRecord<string, string>Request headers
methodstringHTTP method
pathstringRequest path
ipstringClient IP address
userUser | nullAuthenticated user (if auth middleware is used)

Get request header value.

req.get(name: string): string | undefined

Example:

const contentType = req.get('content-type');
const auth = req.get('authorization');

The response object represents the HTTP response.

Send JSON response.

res.json(data: any, status?: number): void

Example:

res.json({ success: true, data: users });
res.json({ error: 'Not found' }, 404);

Send text response.

res.send(data: string, status?: number): void

Set response status code.

res.status(code: number): Response

Example:

res.status(201).json({ created: true });

Redirect to another URL.

res.redirect(url: string, status?: number): void

JWT authentication middleware.

Configure JWT authentication.

JWTAuth(options: JWTOptions): Middleware

Options:

ParameterTypeRequiredDescription
secretstringYesJWT secret key
expiresInstringNoToken expiration (e.g., ‘7d’)
algorithmsstring[]NoAllowed algorithms

Generate JWT token.

JWTAuth.sign(payload: object, expiresIn?: string): string

Example:

const token = JWTAuth.sign(
{ userId: user.id, role: user.role },
'24h'
);

Middleware to require authentication.

JWTAuth.require(): Middleware

import { Database } from 'nexaspace';
const db = new Database({
type: 'postgresql',
host: 'localhost',
port: 5432,
database: 'nexaspace',
username: 'user',
password: 'pass'
});

Execute raw SQL query.

db.query<T>(sql: string, params?: any[]): Promise<T[]>

Insert record.

db.insert(table: string, data: object): Promise<number>

Update records.

db.update(table: string, data: object, where: object): Promise<number>

Delete records.

db.delete(table: string, where: object): Promise<number>

import { Cache } from 'nexaspace';
const cache = new Cache({
type: 'redis',
host: 'localhost',
port: 6379,
ttl: 3600 // Default TTL in seconds
});

Get cached value.

cache.get<T>(key: string): Promise<T | null>

Set cached value.

cache.set(key: string, value: any, ttl?: number): Promise<void>

Delete cached value.

cache.del(key: string): Promise<void>

Clear all cached values.

cache.clear(): Promise<void>

Request handler function type.

type Handler = (
req: Request,
res: Response,
next?: NextFunction
) => void | Promise<void>

Middleware function type.

type Middleware = (
req: Request,
res: Response,
next: NextFunction
) => void | Promise<void>

Next middleware function type.

type NextFunction = (error?: Error) => void