-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
64 lines (49 loc) · 1.44 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'use strict'
const path = require('path')
const serveStatic = require('serve-static')
const express = require('express')
const app = express()
const PORT = process.env.PORT || 8081
const EXTENSION = ['/*.min.js', '/*.min.css', '*.png', '*.svg']
const EXTENSION_JS = 'js'
const EXTENSION_CSS = 'css'
const EXTENSION_PNG = 'png'
const EXTENSION_SVG = 'svg'
app.get(EXTENSION, (request, response, next) => {
const extension = request.originalUrl.split('.').pop()
request.url = `${request.url}.gz`
response.set('Content-Encoding', 'gzip')
switch (extension) {
case EXTENSION_JS:
response.set('Content-Type', 'text/javascript')
break
case EXTENSION_CSS:
response.set('Content-Type', 'text/css')
break
case EXTENSION_PNG:
response.set('Content-Type', 'image/png')
break
case EXTENSION_SVG:
response.set('Content-Type', 'image/svg+xml')
break
default:
break
}
next()
})
function setCustomCacheControl(res, path) {
if (serveStatic.mime.lookup(path) === 'text/html') {
res.setHeader('Cache-Control', 'public, max-age=0')
}
}
const oneYear = 3600000 * 24 * 360 // un año
const CONFIG_FILES = {
immutable: true,
maxAge: oneYear,
setHeaders: setCustomCacheControl
}
app.use(express.static('./public', CONFIG_FILES))
app.use('*', express.static(path.resolve(__dirname, 'public')))
app.listen(PORT, () => {
console.log(`Server successful : http://127.0.0.1:${PORT}`)
})