I want to know how to compress and transfer files such as js in node.js.

Asked 2 years ago, Updated 2 years ago, 44 views

I asked this question several times and deleted it, and after searching separately, I found out that the process I wanted was compression transmission. So the conclusion is

I want to know about the compression transmission method using gzip. I don't know because I haven't learned anything.

node.js javascript css

2022-09-22 12:28

2 Answers

The gzip is handled by Web Server Settings. If you're using Apache, read this article if you're using NGINX, read this article.


2022-09-22 12:28

Just to give you the basics

https://nodejs.org/api/zlib.html

You can use zlib

Pipe to a file stream object that reads a file You can connect gzip and response objects sequentially.

And in order to get it right on your browser, Write 'Content-Encoding':'gzip' for the response header value.

const zlib = require('zlib');
const http = require('http');
const fs = require('fs');
http.createServer((request, response) => {
    const raw = fs.createReadStream('something.js');
    response.writeHead(200, { 'Content-Encoding': 'gzip' });
    raw.pipe(zlib.createGzip()).pipe(response);
}).listen(3000);

All this is possible with nodes alone, of course

At the actual service level, the additional settings for these static resources are: As @Yupto-gun said, it is more common to process it with a web server setting.


2022-09-22 12:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.