@fran-834/gs-microservice-core
Version:
Core package for Node.js microservices by Galduria Software. Includes security, logging, validation, and error handling middlewares.
63 lines (46 loc) • 2 kB
Markdown
# Galduria Software Microservices Core
This package provides the core utilities, middlewares, and helpers for Node.js microservices at Galduria Software. It includes security, logging, validation, and error handling features to ensure consistency and best practices across all microservices.
## Features
- Express middleware for security (helmet, CORS)
- Centralized error handling and standard response format
- JWT authentication middleware
- Request logging with Winston and Morgan
- JSON schema validation with AJV
## Installation
```bash
npm install @fran-834/gs-microservice-core
```
## Usage Example
```ts
import express from "express";
import {
setupCoreMiddlewares,
setupErrorHandler,
verifyToken,
standardResponse,
logInfo,
logError,
} from "@fran-834/gs-microservice-core";
const app = express();
// Initialize core middlewares (security, logging, request ID, etc.)
setupCoreMiddlewares(app, {
jwtSecret: process.env.JWT_SECRET!, // optional JWT_SECRET required
helmetConfig: {}, // optional Helmet options
corsOptions: {}, // optional CORS options
gateway: false, // optional gateway mode
});
app.get("/secure", verifyToken, (req, res) => {
standardResponse(res, 200, "Secure endpoint", { userId: req.userId });
});
setupErrorHandler(app);
app.listen(3000, () => logInfo("Service running on port 3000"));
```
## API
- `setupCoreMiddlewares(app, options)` – Sets up security, logging, and request ID middleware. `options` is an object: `{ jwtSecret?: string, helmetConfig?: HelmetOptions, corsOptions?: CorsOptions, gateway?: boolean }`.
- `setupErrorHandler(app)` – Adds centralized error handling middleware.
- `verifyToken(req, res, next)` – Express middleware to validate JWT tokens.
- `standardResponse(res, code, message, data, ...)` – Sends a standardized JSON response.
- `validateSchema(schema, data)` – Validates data against a JSON schema using AJV.
- Logging helpers: `logInfo`, `logError`, `logDebug`, `logOperation`.
## License
ISC