-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
87 lines (76 loc) · 2.71 KB
/
index.ts
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
/**
* This moduel works as router for backend.
* If the address is not /api/*, this router passes the connection
* to react application.
*/
import express from 'express'
import { Request, Response} from "express"
import 'reflect-metadata'
import { createConnection } from 'typeorm'
import path from 'path'
import bodyParser from 'body-parser'
let cookieParser = require('cookie-parser')
import { testRouter } from './routes/tests'
import { userRouter } from './routes/user.route'
import { adminRouter } from './routes/admin.route'
import { authRouter } from './routes/auth.route'
import { ValidateController } from './api/controllers/validate.controller'
/**
* secretKey is used in jwt authentication encryption.
*/
const app = express()
console.log(process.env.HOKUI_SECRET)
console.log(process.env.HOKUI_PW)
app.set("secretKey", process.env.HOKUI_SECRET)
/**
* In developmental and production stage, CORS policy errors should be
* avoided. These setHeader methods are placed for this purpose.
* If you add specific header, you should also its key header value
* to Access-Control-Allow-Headers.
*/
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*") // update to match the domain you will make the request from
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
res.setHeader("Access-Control-Allow-Methods", "POST, GET")
res.setHeader("Access-Control-Max-Age", "3600")
res.setHeader("Access-Control-Allow-Headers", "Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers")
next()
})
/**
* Serve the static files from the React app
*/
app.use(express.static(path.join(__dirname, '/../client/build')))
/**
+ body parser for json
*/
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(cookieParser())
/**
* Routing part.
*/
app.use('/api/test', testRouter )
app.use('/api/auth', authRouter)
app.use('/api/user', ValidateController.validateUser, userRouter)
app.use('/api/admin',ValidateController.validateAdmin, adminRouter)
app.get('/api/*', (req:Request, res:Response) => {
res.json({status:404})
})
/**
* Handles any requests that don't match the ones above
*/
app.get('*', (req:Request,res:Response) =>{
res.sendFile(path.join(__dirname, '/../client/build/index.html'))
})
/**
* createConnection is one of typeorm function.
* Database connection should be one, and access
* via getManager function in * typeORM.
*/
createConnection()
.then(async connection => {
const port = 3000
app.listen(port);
console.log('App is listening on port ' + port);
})
.catch(error => console.log("TypeORM connection error: ", error));