2017-06-21 52 views
0

我使用compression-webpack-plugin在我的包过程中制作gzip文件,因此捆绑完成后,我的dist文件夹中有这样的文件。提供预先制作的gzip文件

bundle.eefaef91f71795847e74.js 
bundle.eefaef91f71795847e74.js.gz 
vendor.jduu4f4lj71759dj7e74.js 
vendor.jduu4f4lj71759dj7e74.js.gz 
stylesLocal.b7ac53d721aca93e4e65099cf74dc90f.css 
stylesLocal.b7ac53d721aca93e4e65099cf74dc90f.css.gz 

服务器内部我使用express-static-gzip来服务我的gzip文件。为什么这不起作用。我的页面甚至不想加载?如果我把Express.static而不是expressStaticGzip它工作正常。

import Express from 'express' 
import path from 'path' 
import conf from './conf' 
import appRenderer from './appRenderer' 
import webpackUtils from './webpackUtils' 
var expressStaticGzip = require('express-static-gzip') 

const APP_PORT: number = conf.APP_PORT 
const PORT: any = process.env.PORT || APP_PORT 

const app: Express = new Express() 

app.set('views', path.join(__dirname, 'views')) 
app.set('view engine', 'ejs') 

app.use(expressStaticGzip(path.join(__dirname, '../', 'dist'))) 

/* check with the server before using the cached resource */ 
app.use((req: Object, res: Object, next:() => void): void => { 
    res.set('Cache-Control', 'no-cache') 
    return next() 
}) 

/* Use server side rendering for first load */ 
app.use(appRenderer) 

/* Use CommonChunks and long term caching */ 
app.use(webpackUtils) 

// Routes 
app.get('*', (req: Object, res: Object) => { 
    res.render('index', {app: req.body}) 
}) 

app.listen(PORT,() => { 
    console.log(` 
    Express server is up on port ${PORT} 
    Production environment 
    `) 
}) 

然后我将它们引用到我的index.ejs文件中。

<!DOCTYPE html> 
<html lang="en"> 

<head> 
    <meta charset="UTF-8" /> 

    <title>D</title> 

     <link rel='stylesheet' type='text/css' href="stylesLocal.b7ac53d721aca93e4e65099cf74dc90f.css"> 
</head> 

    <body> 

     <div id="app"><%- app %></div> 
     <script src="vendor.jduu4f4lj71759dj7e74.js"></script> 
     <script src="bundle.eefaef91f71795847e74.js"></script> 

    </body> 
</html> 

回答

0

readme of express-static-gzip解释的,语法是从express.static略有不同。取而代之的

app.use(expressStaticGzip(path.join(__dirname, '../', 'dist'))) 

尝试

app.use('/', expressStaticGzip(path.join(__dirname, '../', 'dist')))