-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
48 lines (40 loc) · 1.35 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
import express, {Express, Request, Response } from "express"
import dotenv from 'dotenv'
import path, {dirname} from 'path'
import { fileURLToPath } from "url"
import apiRouter from './routes/apiRouter.ts'
import cors from 'cors'
// env
dotenv.config()
const corsOptions = {
origin: ['http://localhost:5173', 'http://localhost:8100'],
optionsSuccessStatus: 200, // some legacy browsers (IE11, various SmartTVs) choke on 204,
exposedHeaders: 'Set-Cookie',
allowedHeaders: 'Authorizaton,Content-Type',
credentials: false,
preflight: false
}
const app: Express = express()
const port = process.env.PORT || 8100
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
app.use(cors(corsOptions))
app.use(express.json())
app.use(express.urlencoded())
app.use(express.static(path.join(__dirname, 'dist', 'index.html')))
app.use('/api', apiRouter)
if (process.env.NODE_ENV == 'development') {
app.get('*', (req: Request, res: Response) => {
console.log(req.path)
res.send("unknown route")
})
} else {
app.use(express.static(path.join(__dirname, 'dist')))
app.get('*', (req :Request, res: Response) => {
console.log(req.path)
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
})
}
const server = app.listen(port, () => {
console.log("listening on port " + port)
})