-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.ts
More file actions
54 lines (44 loc) · 1.36 KB
/
server.ts
File metadata and controls
54 lines (44 loc) · 1.36 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
import express = require('express')
import mongoose = require('mongoose')
import path = require('path')
import bodyParser = require('body-parser')
require('dotenv').config()
const { PORT = 4000, MONGODB_URL } = process.env
mongoose
.connect(MONGODB_URL as string, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
})
.then(() => {
console.log('Connected to MongoDB')
})
.catch(console.error)
//express likes to call the server "app"
const app = express()
app.use(function (req: any, res: any, next: any) {
res.header('Access-Control-Allow-Origin', 'http://localhost:3000')
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept'
)
res.header(
'Access-Control-Allow-Methods',
'PUT, POST, GET, DELETE, OPTIONS, PATCH'
)
next()
})
app.use(bodyParser.json({ limit: '50mb' }))
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }))
app.use('/', express.json()) // (req, res, next) => {...}
app.use('/api/users', require('./routes/users'))
app.use(express.static(path.join(__dirname, './client/build')))
// redirect to index.html
app.get('*', (req: any, res: any) => {
res.sendFile(path.join(__dirname, './client/build'))
})
// error route
app.use(require('./routes/error'))
app.listen(PORT, () => {
console.log(`Server started at http://localhost:${PORT}`)
})