1.0.1 • Published 6 months ago
shutgrace v1.0.1
shutgrace
Graceful shutdown of Node.js servers with SQL database
The shutgrace
package helps you gracefully shut down a Node.js server that connects to a SQL database.
Setup
Install the package:
npm install shutgrace
Usage
import express from 'express';
import gracefulShutdown from 'shutgrace';
import pinoLogger from 'lib/logger.js';
import { AppDataSource } from 'config/appDataSource.js'; // example TypeORM data source; optional
const app = express()
const port = 3000
AppDataSource.initialize()
app.get('/', (req, res) => {
res.send('Hello World!')
})
const server = app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
process.on('SIGINT', () => gracefulShutdown('SIGINT', server, { dataSource: AppDataSource, logger: pinoLogger }));
process.on('SIGTERM', () => gracefulShutdown('SIGTERM', server, { dataSource: AppDataSource, logger: pinoLogger }));
Optional arguments
- "dataSource": needs tor responde to
destroy()
(ex. AppDataSource). - "logger": needs tor responde to
error()
(ex. Pino).
TypeScript Support
This package includes TypeScript type definitions. If you use TypeScript, you automatically get type-checking for the gracefulShutdown
function.
License
This project is licensed under the MIT License.
Package URL
https://www.npmjs.com/package/shutgrace
Code repository
https://bitbucket.org/endeavia/shutgrace/src/master/