-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
57 lines (47 loc) · 1.39 KB
/
main.js
File metadata and controls
57 lines (47 loc) · 1.39 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
import 'dotenv/config'
import express from 'express'
import cors from 'cors'
import { connect } from './utils/dbConnect.js'
import apiRouter from './routes/index.js'
import chalk from 'chalk'
import csrf from 'csurf';
import helmet from 'helmet'; // Add this line
import cookieParser from 'cookie-parser'
import bodyParser from 'body-parser'
const portCon = chalk.blue.bgWhite.bold
const App = express()
App.use(express.json())
App.use(bodyParser.urlencoded({ extended: true }))
App.use(bodyParser.json());
App.use(cookieParser());
App.use(cors({ origin: 'http://localhost:3000' }))
App.disable('x-powered-by')
// Enable CSRF protection
const csrfProtection = csrf({ cookie: { key: '_csrf' } });
// Use the CSRF middleware before the routes
App.use(csrfProtection);
App.use(
helmet({
frameguard: {
action: 'deny', // Use 'deny' to prevent framing entirely
},
// Content Security Policy with frame-ancestors directive:
contentSecurityPolicy: {
directives: {
'frame-ancestors': "'none'", // Use "'none'" to disallow all framing
},
},
noSniff: true, // Enable X-Content-Type-Options: nosniff
}),
)
connect()
App.get('/csrf-token', (req, res) => {
res.send(req.csrfToken() )
})
App.use('/api', apiRouter)
const port = process.env.PORT || 3001
App.listen(
port,
console.log(portCon(`🚀 Server listening on PORT ${process.env.PORT} 🚀`)),
)
export default App