-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·100 lines (87 loc) · 2.21 KB
/
Copy pathserver.js
File metadata and controls
executable file
·100 lines (87 loc) · 2.21 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env node
import process from 'node:process'
import express from 'express'
import helmet from 'helmet'
import cors from 'cors'
import compression from 'compression'
import morgan from 'morgan'
import dotenv from 'dotenv'
import mongoSanitize from 'express-mongo-sanitize'
import xss from 'xss'
import hpp from 'hpp'
import crypto from 'node:crypto'
dotenv.config()
const app = express()
app.use(helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ['\'self\''],
styleSrc: ['\'self\'', '\'unsafe-inline\'', 'https://fonts.googleapis.com'],
fontSrc: ['\'self\'', 'https://fonts.gstatic.com'],
scriptSrc: ['\'self\''],
imgSrc: ['\'self\'', 'data:', 'https:'],
connectSrc: ['\'self\''],
frameSrc: ['\'none\''],
objectSrc: ['\'none\''],
mediaSrc: ['\'self\''],
workerSrc: ['\'none\'']
}
},
hsts: {
maxAge: 31536000,
includeSubDomains: true,
preload: true
}
}))
app.use(cors())
app.use(compression())
app.use(morgan('dev'))
app.use(express.json({
limit: '10mb',
verify: (req, _res, buf) => {
req.rawBody = buf
}
}))
app.use(mongoSanitize({
replaceWith: '_'
}))
app.use(hpp())
app.use((req, _res, next) => {
if (req.body && typeof req.body === 'object' && !Array.isArray(req.body)) {
const entries = Object.entries(req.body).map(([key, value]) => {
if (key === '__proto__' || key === 'constructor') return [key, value]
const sanitizedValue = typeof value === 'string' ? xss(value) : value
return [key, sanitizedValue]
})
req.body = Object.fromEntries(entries)
}
next()
})
app.get('/api/health', (_req, res) => {
res.json({ status: 'ok' })
})
app.use((req, res, next) => {
req.id = crypto.randomUUID()
req.timestamp = new Date().toISOString()
res.setHeader('X-Request-ID', req.id)
next()
})
app.get('/api/wheel/stages', async (_req, res) => {
const stages = [
{
id: 1,
title: 'Creative Remembering',
symbol: '🌱'
}
]
res.json({
success: true,
data: stages,
timestamp: new Date().toISOString()
})
})
const PORT = process.env.PORT || 4200
app.listen(PORT, () => {
process.stdout.write('Server running on port ' + PORT + '\n')
})
export default app