Add static option

This commit is contained in:
Typicode
2015-09-19 10:48:56 +02:00
parent 6610adbf05
commit 6c6333fa6d
9 changed files with 97 additions and 37 deletions

View File

@ -1,41 +1,48 @@
var fs = require('fs')
var path = require('path')
var express = require('express')
var logger = require('morgan')
var cors = require('cors')
var errorhandler = require('errorhandler')
var arr = []
module.exports = function (opts) {
var userDir = path.join(process.cwd(), 'public')
var defaultDir = path.join(__dirname, 'public')
var staticDir = fs.existsSync(userDir) ?
userDir :
defaultDir
// Logger
arr.push(logger('dev', {
skip: function (req, res) {
return process.env.NODE_ENV === 'test' ||
req.path === '/favicon.ico'
opts = opts || { static: staticDir }
var arr = []
// Logger
arr.push(logger('dev', {
skip: function (req, res) {
return process.env.NODE_ENV === 'test' ||
req.path === '/favicon.ico'
}
}))
// Enable CORS for all the requests, including static files
arr.push(cors({ origin: true, credentials: true }))
if (process.env.NODE_ENV === 'development') {
// only use in development
arr.push(errorhandler())
}
}))
// Enable CORS for all the requests, including static files
arr.push(cors({ origin: true, credentials: true }))
// Serve static files
arr.push(express.static(opts.static))
if (process.env.NODE_ENV === 'development') {
// only use in development
arr.push(errorhandler())
// No cache for IE
// https://support.microsoft.com/en-us/kb/234067
arr.push(function (req, res, next) {
res.header('Cache-Control', 'no-cache')
res.header('Pragma', 'no-cache')
res.header('Expires', '-1')
next()
})
return arr
}
// Serve static files
if (fs.existsSync(process.cwd() + '/public')) {
arr.push(express.static(process.cwd() + '/public'))
} else {
arr.push(express.static(__dirname + '/public'))
}
// No cache for IE
// https://support.microsoft.com/en-us/kb/234067
arr.push(function (req, res, next) {
res.header('Cache-Control', 'no-cache')
res.header('Pragma', 'no-cache')
res.header('Expires', '-1')
next()
})
module.exports = arr