-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
61 lines (53 loc) · 1.36 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import express from 'express';
import cors from 'cors';
import router from './routes/index.js';
import logger from 'morgan';
import MORGAN_FORMAT from './config/logger.js';
import helmet from 'helmet';
import path from 'path';
import { fileURLToPath } from 'url';
const app = express();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
//* configuration to allow all origins and specific methods and headers.
app.use(
cors({
origin: '*',
methods: 'GET, POST, PUT, PATCH, DELETE',
allowedHeaders: 'Content-Type, Authorization',
})
);
app.use(helmet());
app.use(express.json());
app.use(logger('dev'));
app.use(express.urlencoded({ extended: true }));
app.use(express.static(__dirname + '/public'));
//* Force the output to be application/json and remove fingerprint
app.use((req, res, next) => {
res.removeHeader('X-Powered-By');
res.setHeader('Content-Type', 'application/json');
next();
});
app.use(router);
//* Error Handler with http-errors
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.json({
status: false,
message: err.message,
});
});
//* 404 Response Handler
app.use((req, res) => {
const url = req.url;
const method = req.method;
res.status(404).json({
status: false,
code: 404,
method,
url,
message: 'Not Found!',
docs: '/documentation',
});
});
export default app;