-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
46 lines (32 loc) · 1.23 KB
/
server.js
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
require('dotenv').config()
const express = require('express')
const path = require('path')
const bodyParser = require('body-parser')
const LinkStoreController = require('./api/controllers/links.store.controller')
const LinkShowController = require('./api/controllers/links.show.controller')
const LinkStatsController = require('./api/controllers/links.stats.show.controller')
app = express()
app.use(bodyParser.json())
app.post('/api/links', LinkStoreController)
app.get('/api/links/:code/stats',LinkStatsController)
app.get('/:code', async (req, res, next) => {
res.set('Cache-Control', 'no-store')
if (req.url.startsWith('/favicon')){
return res.status(404).json()
}
LinkShowController(req,res,next)
}, (req, res) => {
let clientDir = __dirname + '/client'
return res.status(404).sendFile(path.join(clientDir,'index.html'))
})
app.get('*', (req, res) => {
let clientDir = __dirname + '/client'
if (req.url.startsWith('/js')){
return res.sendFile(path.join(clientDir,'dist',req.url))
}
if (req.url.startsWith('/css')){
return res.sendFile(path.join(clientDir,'dist',req.url))
}
return res.sendFile(path.join(clientDir,'index.html'))
})
app.listen(process.env.PORT || 3000)