简体   繁体   中英

Enable gzip for node app running on Azure

I have a node app with a React front-end running on Node 14 as an Azure App Service.

The app consists of a middleman node server (on port 3000, running on express) that routes all API requests to another service (a.Net app running on a separate Azure App Service), and a React front-end that the user interacts with (running on port 3001). Everything seems to be working fine - both the local dev environment and the Azure service handle API requests as they should.

Looking a little closer, however, it seems that the local dev environment (typically running on a Mac) is gzipping all responses from the API, whereas the Azure service isn't . This is resulting in a ~90% increase in the size of the data that the middleman server is sending to the front-end React app when running in Azure as opposed to locally. For example, one of our API calls returns a response weighing ~33kB when running locally, but an identical request returns a ~500kB response.

I've tried specifying Accept-Encoding and Content-Type header, but this has no effect.

My initial thoughts are that the Azure service is configured differently to the local one, and that gzipping either isn't supported on Azure or hasn't been explicitly enabled. Alternatively, the Azure instance might be going through some kind of gateway that's stripping content encoding headers?

Does anyone have any insights into what I should try to get Azure to send gzipped responses?

It turns out that I missed a small (but crucial) step: enabling compression on the Express server.

Installing and importing the Compression package, and adding the following to our server code solved the issue:

app.use(compression());

Without this the locally-running front-end was serving compressed data, but not the Azure-deployed version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM