Node.js Express无法提供预压缩的gzip文件,可能是因为路径错误

Node.js Express无法提供预压缩的gzip文件,可能是因为路径错误,node.js,express,gzip,Node.js,Express,Gzip,我有一个文件夹结构如下的React应用程序: ├── index.html └── app ├── server.js ├── routes.jsx ├── scripts │ ├── bundle.js │ ├── bundle.js.gz │ ├── vendor.js │ └── vendor.js.gz └── components └── ... 当.js文件被请求时,我需要提供预压缩文

我有一个文件夹结构如下的React应用程序:

├── index.html
└── app
    ├── server.js
    ├── routes.jsx
    ├── scripts
    │   ├── bundle.js
    │   ├── bundle.js.gz
    │   ├── vendor.js
    │   └── vendor.js.gz
    └── components
        └──  ...
当.js文件被请求时,我需要提供预压缩文件(*.js.gz),但是原始的.js文件被提供。这可能是因为走错了路,但我想不出来

这是我的server.js文件:

import React from 'react';
import ReactDOMServer from 'react-dom/server';
import { StaticRouter } from 'react-router';
import http from 'http';
import express from 'express';
import fs from 'fs';
import App from '~/routes.jsx';

const index = fs.readFileSync('index.html', 'utf8');
const PORT = process.env.PORT || 8000;

const app = new express();
const server = new http.Server(app);

app.use('/app', express.static('app'));

app.use((request, response) => {
    const context = {};

    const html = ReactDOMServer.renderToString(
        <StaticRouter location={request.url} context={context}>
            <App/>
        </StaticRouter>
    );

    if (context.url) {
        response.writeHead(301, {Location: context.url});
        response.end();
    } else {
        response.write(index.replace(
            /<div id="root"><\/div>/,
            `<div id="root">${html}</div>`
        ));
        response.end();
    }
});

app.get('*.js', function (request, response, next) {
    console.log('js requested');
    request.url = request.url + '.gz';
    response.set('Content-Encoding', 'gzip');
    next();
});

server.listen(PORT);
console.log(`\nApplication available at localhost:${PORT}\n`);
从“React”导入React;
从“react dom/server”导入ReactDOMServer;
从“react router”导入{StaticRouter};
从“http”导入http;
从“快递”进口快递;
从“fs”导入fs;
从“~/routes.jsx”导入应用程序;
const index=fs.readFileSync('index.html','utf8');
const PORT=process.env.PORT | 8000;
const app=new express();
const server=新的http.server(app);
app.use('/app',express.static('app'));
应用程序使用((请求、响应)=>{
const context={};
const html=ReactDOMServer.renderToString(
);
if(context.url){
writeHead(301,{Location:context.url});
response.end();
}否则{
response.write(index.replace(
//,
`${html}`
));
response.end();
}
});
app.get('*.js',函数(请求、响应、下一步){
log('jsrequested');
request.url=request.url+'.gz';
set('Content-Encoding','gzip');
next();
});
监听(端口);
log(`\n本地主机上可用的应用程序:${PORT}\n`);

Express中的中间件始终按照您添加它们的顺序调用。因此,要加载
*.js.gz
文件,需要将中间件移动到
express.static
中间件

//...
//load this middleware first
app.get('*.js', function (request, response, next) {
    console.log('js requested');
    request.url = request.url + '.gz';
    response.set('Content-Encoding', 'gzip');
    next();
});
//then load the express.static middleware
app.use('/app', express.static('app'));
//...

Express中的中间件总是按照您添加它们的顺序进行调用。因此,要加载
*.js.gz
文件,需要将中间件移动到
express.static
中间件

//...
//load this middleware first
app.get('*.js', function (request, response, next) {
    console.log('js requested');
    request.url = request.url + '.gz';
    response.set('Content-Encoding', 'gzip');
    next();
});
//then load the express.static middleware
app.use('/app', express.static('app'));
//...

成功了。非常感谢你。我真的很生气。文件送达正确,所以你的建议有效,但还有一个问题。我得到304未修改错误。你知道这件事吗?好的,我解决了。忘了我在这里的独白吧:)成功了。非常感谢你。我真的很生气。文件送达正确,所以你的建议有效,但还有一个问题。我得到304未修改错误。你知道这件事吗?好的,我解决了。忘了我的独白吧:)