feat: inisialisasi server Express dengan Node.js, TypeScript, dan middleware keamanan
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
# Server Configuration
|
||||
PORT=5000
|
||||
NODE_ENV=development
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
# Dependencies
|
||||
node_modules/
|
||||
|
||||
# Production Build Output
|
||||
dist/
|
||||
|
||||
# Local Environment variables
|
||||
.env
|
||||
.env.local
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
|
||||
# Logs
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
lerna-debug.log*
|
||||
*.log
|
||||
|
||||
# Editor & IDE settings
|
||||
.vscode/
|
||||
.idea/
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
||||
|
||||
# OS metadata
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
Generated
+2356
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"name": "internify-be",
|
||||
"version": "1.0.0",
|
||||
"description": "Internify Backend Server",
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
"build": "rimraf dist && tsc",
|
||||
"start": "node dist/index.js",
|
||||
"dev": "ts-node-dev --respawn --transpile-only src/index.ts"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^16.4.5",
|
||||
"express": "^4.19.2",
|
||||
"helmet": "^7.1.0",
|
||||
"morgan": "^1.10.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/cors": "^2.8.17",
|
||||
"@types/express": "^4.17.21",
|
||||
"@types/morgan": "^1.9.9",
|
||||
"@types/node": "^20.12.12",
|
||||
"rimraf": "^5.0.7",
|
||||
"ts-node-dev": "^2.0.0",
|
||||
"typescript": "^5.4.5"
|
||||
}
|
||||
}
|
||||
+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;
|
||||
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es2022",
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es2022"
|
||||
],
|
||||
"allowJs": true,
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"strict": true,
|
||||
"noImplicitAny": true,
|
||||
"strictNullChecks": true,
|
||||
"strictFunctionTypes": true,
|
||||
"noImplicitThis": true,
|
||||
"alwaysStrict": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"noImplicitReturns": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"include": [
|
||||
"src/**/*"
|
||||
],
|
||||
"exclude": [
|
||||
"node_modules",
|
||||
"dist"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user