0

I created simple app used angular elements and http node server. To optimize bundle size i just convert the resulting js file to gz format with current command:

"postbuild": "cat dist/cs6-user-widget/{runtime,polyfills,polyfills-es5,scripts,main}.js | gzip > webcomponent/user-widget.js.gz" 

The file is created correctly, but i cannot serve this type of content with my node server:

const http = require('http'); const fs = require('fs'); const hostname = 'localhost'; const port = 4202; http.createServer((request, response) => { var contentType = 'text/html'; var filePath = './' + request.url; fs.readFile(filePath, function(error, content) { if (error) { fs.readFile('./index.html', function(error, content) { response.writeHead(200, { 'content-encoding': 'gzip', 'Content-Type': contentType }); return response.end(content, 'utf-8'); }); } else { response.writeHead(200, { 'content-encoding': 'gzip', 'Content-Type': contentType }); return response.end(content, 'utf-8'); } }); }); 

Can You help me to correctly implement node part?

1
  • The web-component/element part has nothing to do with your question; Your question is: How can I serve a GZipped file with NodeJS Commented Dec 31, 2020 at 16:50

1 Answer 1

0

First, please see this answer

If you still want to use node, here is a simple example:

const express = require("express"); const gzipStatic = require("connect-gzip-static"); const app = express(); app.use(gzipStatic(__dirname)); app.listen(4000); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.