Skip to content

Instantly share code, notes, and snippets.

@andreasvirkus
Created September 17, 2017 21:55
Show Gist options
  • Save andreasvirkus/5046f369274c875e681654bb49e396e6 to your computer and use it in GitHub Desktop.
Save andreasvirkus/5046f369274c875e681654bb49e396e6 to your computer and use it in GitHub Desktop.
export const serve = (path, cache) => express.static(resolve(path), {
maxAge: cache && isProd ? 60 * 60 * 24 * 30 : 0
})
// Usage
import { serve } from './express-serve-static.js'
const fs = require('fs')
const path = require('path')
const express = require('express')
const favicon = require('serve-favicon')
const compression = require('compression')
const app = express()
const port = process.env.PORT || 8080
const isProd = process.env.NODE_ENV === 'production'
app.use(compression({ threshold: 0 }))
app.use(favicon('./public/logo.png'))
app.use('/service-worker.js', serve('./dist/service-worker.js'))
app.use('/manifest.json', serve('./manifest.json'))
app.use('/dist', serve('./dist'))
app.use('/public', serve('./public'))
app.listen(port, () => {
console.log(`server started at localhost:${port}`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment