nhb-express
Version:
Express TypeScript Server Scaffold
54 lines (44 loc) ⢠1.11 kB
text/typescript
import configs from '@/configs';
import { connectDB } from '@/configs/db';
import chalk from 'chalk';
import type { Server } from 'http';
import app from './app';
let server: Server;
const bootStrap = async () => {
try {
// Connect to DB
await connectDB();
// Listen to the Server
server = app.listen(configs.port, () => {
console.info(
chalk.yellowBright(`š Server is Listening on Port: ${configs.port}`)
);
});
} catch (error) {
if (error instanceof Error) {
console.error(chalk.red(`š« Error Occurred: ${error.message}`));
} else {
console.error(chalk.red('š Unknown Error Occurred!'));
}
}
};
bootStrap().catch(console.dir);
process.on('unhandledRejection', (err) => {
console.error(
chalk.redBright(
`š« Unhandled Rejection Detected!\nš Server is Shutting Down... ${err}`
)
);
if (server) {
server.close(() => {
process.exit(1);
});
}
process.exit(1);
});
process.on('uncaughtException', () => {
console.error(
chalk.redBright(`š« Uncaught Exception Detected!\nš Server is Shutting Down...`)
);
process.exit(1);
});