Gland is a powerful and flexible Node.js framework designed to streamline the development of web applications and APIs. It combines a minimalist approach with advanced features, providing developers with the tools they need to build efficient and scalable applications.
Overview
Gland offers a modern web server framework with a focus on simplicity and performance. It is designed to be highly customizable, making it suitable for a wide range of applications, from simple REST APIs to complex web services. Here are some of the standout features of Gland:
- Flexible Routing: Gland’s routing system supports various HTTP methods and custom routes, allowing developers to define endpoints with precision.
- Middleware Support: The framework includes a robust middleware system that can be applied globally, at the route level, or within specific methods.
- Integration with Qiu and Logger: Gland integrates seamlessly with Qiu for SQL database operations and provides built-in logging capabilities through its logger component.
Core Features
1. Flexible Routing
Gland’s routing system is designed to be both powerful and intuitive. It supports various HTTP methods such as GET, POST, PUT, DELETE, PATCH, and more. Developers can define routes using decorators, making it easy to map HTTP requests to specific handlers.
Example Usage:
import gland, { Get, Post, Route, Context } from '@medishn/gland'; @Route('/')
class TestController { @Get() public async get(ctx: Context) { ctx.write('Hello, world!'); ctx.end(); } @Post('/data') public async postData(ctx: Context) { const data = await ctx.json(); ctx.write(`Received data: ${JSON.stringify(data)}`); ctx.end(); }
} const app = new gland();
app.use('/', TestController);
app.listen(3000, () => { console.log('Server is running on port 3000');
});
2. Middleware Support
Gland offers a flexible middleware system that allows you to apply middleware globally, at the route level, or for specific methods. This enables you to handle cross-cutting concerns such as authentication, logging, and request validation efficiently.
- Global Middleware: Applied to all routes and methods.
- Route Middleware: Applied to specific routes.
- Method Middleware: Applied to specific methods within a route.
Example Usage:
import gland, { Context, Get, Route, mid, mids } from '@medishn/gland'; // Global Middleware
const globalMiddleware = async (ctx: Context, next: Function) => { console.log('Global middleware'); await next();
}; // Route Middleware
const routeMiddleware = async (ctx: Context, next: Function) => { console.log('Route middleware'); await next();
}; @Route('/')
@mids(globalMiddleware) // Applies to all methods in this class
class TestController { @mid(routeMiddleware) // Applies only to this method @Get('/hello') public async hello(ctx: Context) { ctx.write('Hello with middleware!'); ctx.end(); }
} const app = new gland();
app.use('/', TestController);
app.listen(3000, () => { console.log('Server is running on port 3000');
});
3. Integration with Qiu
Gland integrates with Qiu, a query runner for SQL databases, supporting MySQL, MariaDB, PostgreSQL, and more. This integration simplifies database operations and provides a consistent API for executing SQL queries.
Example Usage:
import gland from '@medishn/gland';
const app = new gland();
const db = new app.Qiu('pg', 'postgresql://username:password@localhost:5432/mydatabase'); async function fetchData() { await db.use('mydatabase'); const results = await db.exec('SELECT * FROM users;'); console.log(results);
} app.listen(3000, () => { console.log('Server is running on port 3000'); fetchData();
});
4. Built-In Logging
Gland includes a built-in logger component that supports various log levels and outputs to different transports, such as files and consoles. The logger can be configured to meet the needs of your application, providing detailed and configurable logging.
Example Usage:
import gland from '@medishn/gland';
const app = new gland();
const logger = app.logger({ level: 'info', transports: ['file', 'console'], file: 'app.log', rotation: { maxSize: 1024 * 1024 }, // 1 MB
}); logger.log('Application started', 'info'); app.listen(3000, () => { console.log('Server is running on port 3000');
});
Getting Started
To get started with Gland, install it via npm or yarn:
npm install @medishn/gland
Or
yarn add @medishn/gland
Once installed, you can start using Gland by creating a new instance of the gland
class and configuring it according to your needs. Refer to the examples and documentation provided to set up routing, middleware, database connections, and logging.
Conclusion
Gland is a versatile and efficient framework that simplifies the process of building web applications and APIs with Node.js. Its combination of flexible routing, robust middleware support, seamless database integration, and built-in logging makes it a powerful tool for developers seeking a comprehensive and customizable framework. For more information and detailed documentation, refer to the official Gland repository.
Discover more from Coursity
Subscribe to get the latest posts sent to your email.