feat: inisialisasi server Express dengan Node.js, TypeScript, dan middleware keamanan
This commit is contained in:
+35
@@ -0,0 +1,35 @@
|
||||
import express, { Request, Response, NextFunction } from 'express';
|
||||
import cors from 'cors';
|
||||
import helmet from 'helmet';
|
||||
import morgan from 'morgan';
|
||||
import routes from './routes';
|
||||
import { errorHandler, CustomError } from './middleware/errorHandler';
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use(helmet());
|
||||
app.use(cors());
|
||||
|
||||
app.use(morgan('dev'));
|
||||
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
|
||||
app.get('/', (_req: Request, res: Response) => {
|
||||
res.status(200).json({
|
||||
message: 'Welcome to the Internify API',
|
||||
documentation: 'Check /api/health untuk status',
|
||||
});
|
||||
});
|
||||
|
||||
app.use('/api', routes);
|
||||
|
||||
app.use((req: Request, _res: Response, next: NextFunction) => {
|
||||
const error: CustomError = new Error(`Not Found - ${req.originalUrl}`);
|
||||
error.statusCode = 404;
|
||||
next(error);
|
||||
});
|
||||
|
||||
app.use(errorHandler);
|
||||
|
||||
export default app;
|
||||
@@ -0,0 +1,31 @@
|
||||
import dotenv from 'dotenv';
|
||||
dotenv.config();
|
||||
|
||||
import app from './app';
|
||||
|
||||
const PORT = process.env.PORT || 5000;
|
||||
const NODE_ENV = process.env.NODE_ENV || 'development';
|
||||
|
||||
const server = app.listen(PORT, () => {
|
||||
console.log(`=========================================`);
|
||||
console.log(` Server jalanin di ${PORT} `);
|
||||
console.log(` Environment: ${NODE_ENV} `);
|
||||
console.log(` API base: http://localhost:${PORT}/api `);
|
||||
console.log(`=========================================`);
|
||||
});
|
||||
|
||||
process.on('unhandledRejection', (err: Error) => {
|
||||
console.error(`[Unhandled Rejection] Shutting down...`);
|
||||
console.error(err.name, err.message);
|
||||
server.close(() => {
|
||||
process.exit(1);
|
||||
});
|
||||
});
|
||||
|
||||
process.on('uncaughtException', (err: Error) => {
|
||||
console.error(`[Uncaught Exception] Shutting down...`);
|
||||
console.error(err.name, err.message);
|
||||
server.close(() => {
|
||||
process.exit(1);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
|
||||
export interface CustomError extends Error {
|
||||
statusCode?: number;
|
||||
}
|
||||
|
||||
export const errorHandler = (
|
||||
err: CustomError,
|
||||
_req: Request,
|
||||
res: Response,
|
||||
_next: NextFunction
|
||||
) => {
|
||||
const statusCode = err.statusCode || 500;
|
||||
const message = err.message || 'Internal Server Error';
|
||||
|
||||
console.error(`[Error] ${statusCode} - ${message}`);
|
||||
if (err.stack) {
|
||||
console.error(err.stack);
|
||||
}
|
||||
|
||||
res.status(statusCode).json({
|
||||
status: 'error',
|
||||
statusCode,
|
||||
message,
|
||||
...(process.env.NODE_ENV === 'development' && { stack: err.stack }),
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,31 @@
|
||||
import { Router, Request, Response, NextFunction } from 'express';
|
||||
|
||||
const router = Router();
|
||||
|
||||
/**
|
||||
* @route GET /api/health
|
||||
* @desc Endpoint untuk memeriksa kesehatan/status server
|
||||
* @access Public
|
||||
*/
|
||||
router.get('/health', (_req: Request, res: Response) => {
|
||||
res.status(200).json({
|
||||
status: 'OK',
|
||||
message: 'Server is running smoothly',
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* @route GET /api/error-test
|
||||
* @desc Mensimulasikan error server internal untuk memverifikasi middleware penanganan error global
|
||||
* @access Public
|
||||
*/
|
||||
router.get('/error-test', (_req: Request, _res: Response, next: NextFunction) => {
|
||||
try {
|
||||
throw new Error('This is a test error to verify our custom global error handler!');
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user