0.2.0 โข Published 1 year ago
@quicore/expressjs v0.2.0
@quicore/expressjs
Provides a basic setup for an Express server with configurable options.
Includes cookie parsing, security headers via Helmet, JSON parsing with raw body capture, error handling, and support for starting both HTTP and HTTPS servers.
Features
- โ Plug-and-play Express server setup
- ๐ Secure by default with Helmet
- ๐ช Cookie parsing with
cookie-parser - ๐งพ JSON parsing with raw body access
- ๐งฑ Optional view engine and proxy trust config
- โก Start HTTP and/or HTTPS server with auto-port fallback for HTTP
- ๐ง Extendable with custom routes and middlewares
- ๐งผ Centralized error handling
Installation
npm install @quicore/expressjsUsage
import { QuicoreExpressServer } from '@quicore/expressjs';
const config = {
webserver: {
http: { port: 3000 },
https: {
enabled: true,
port: 3443,
ssl: {
key: './ssl/server.key',
cert: './ssl/server.cert',
},
},
express: {
cookie: true,
helmet: true,
urlencodedExtended: true,
views: {
path: './views',
engine: 'ejs',
},
proxy: {
trust: true,
},
},
},
};
const server = new QuicoreExpressServer(config);
// Optional: Add custom middleware
server.use((req, res, next) => {
console.log(`[${req.method}] ${req.url}`);
next();
});
// Optional: Add routes
server.setRoutes([
{
type: 'get',
path: '/',
handlers: (req, res) => res.send('Hello from Quicore Express Server!'),
},
]);
// Start both HTTP and HTTPS servers
server.initializeCommonMiddlewares();
server.startServer();API
Constructor
new QuicoreExpressServer(config?: object)config.webserver.http.port: Port for HTTP server (default: 3000)config.webserver.https: Configuration for HTTPS serverconfig.express: Express middleware settings
Methods
| Method | Description |
|---|---|
initializeCommonMiddlewares() | Sets up JSON, URL-encoded, text, raw, cookie-parser, helmet, and view engine |
use(...middlewares) | Adds middleware(s) to the app |
setRoutes(routes) | Sets up defined route handlers |
startHTTP() | Starts an HTTP server with fallback if port is in use |
startHTTPS() | Starts an HTTPS server with SSL settings |
startServer() | Calls both startHTTP() and startHTTPS(), and sets the error handler |
setAdditionalSupportedContents(contentTypes) | Adds custom content types to parse as raw text |
Error Handling
All errors are logged with context (path, IP, headers, etc.) and return a structured JSON error response:
{
"error": "server_error",
"error_description": "Internal Server Error"
}License
MIT