admin管理员组文章数量:1379419
I'm currently working on a nodeJS implementation(I'm new with node). Basically I have created an API Gateway with a Lambda Function(GET REST service) to query all the records for a postgreSQL table. My implementation is working. Now I started to read about HTTP compression
and I'm trying to implement it in the most natural way. I found a Lib called compression. The thing is I'm not using express
in my implementation. All is native with nodeJS. I would like to know if is it possible to use compression lib without express. Right now I'm compressing the response using zlib.deflateSync
but when I run this in postman a base64 string is returned and I saw some examples where the compressed resposne is automatically transformed by postman or for any browser with the corresponding header. I would like to do the same with my implementation. Can you please give me an advice about if this is possible?
This is part of my code:
import { APIGatewayProxyEvent } from 'aws-lambda'
import { getInfoFromDB } from './db/dbQueries'
import crypto from 'crypto'
import { transformInfoFromDBToJson } from './utils/transformationUtils'
import zlib from 'zlib'
export const handler = async (event: APIGatewayProxyEvent, context: any) => {
try {
let responseBody = JSON.stringify(transformInfoFromDBToJson(await getInfoFromDB()))
// generate ETag from fresh data and set the ETag header.
const freshETag = crypto.createHash('md5').update(responseBody).digest('hex')
const MAX_AGE = process.env.CACHE_MAX_AGE
const cacheControl = `max-age=${MAX_AGE}, must-revalidate`
const headers: { [key: string]: string } = {
ETag: freshETag,
'Cache-Control': cacheControl,
}
if (process.env.COMPRESS_RESPONSE === 'true') {
responseBody = zlib.deflateSync(responseBody).toString('base64')
headers['Content-Encoding'] = 'gzip, deflate'
}
return {
statusCode: 200,
body: responseBody,
headers,
}
} catch (error) {
console.error('Error fetching info:', error)
return {
statusCode: 500,
body: JSON.stringify({ message: 'Error fetching info: Internal Server Error' }),
}
}
}
I'm currently working on a nodeJS implementation(I'm new with node). Basically I have created an API Gateway with a Lambda Function(GET REST service) to query all the records for a postgreSQL table. My implementation is working. Now I started to read about HTTP compression
and I'm trying to implement it in the most natural way. I found a Lib called compression. The thing is I'm not using express
in my implementation. All is native with nodeJS. I would like to know if is it possible to use compression lib without express. Right now I'm compressing the response using zlib.deflateSync
but when I run this in postman a base64 string is returned and I saw some examples where the compressed resposne is automatically transformed by postman or for any browser with the corresponding header. I would like to do the same with my implementation. Can you please give me an advice about if this is possible?
This is part of my code:
import { APIGatewayProxyEvent } from 'aws-lambda'
import { getInfoFromDB } from './db/dbQueries'
import crypto from 'crypto'
import { transformInfoFromDBToJson } from './utils/transformationUtils'
import zlib from 'zlib'
export const handler = async (event: APIGatewayProxyEvent, context: any) => {
try {
let responseBody = JSON.stringify(transformInfoFromDBToJson(await getInfoFromDB()))
// generate ETag from fresh data and set the ETag header.
const freshETag = crypto.createHash('md5').update(responseBody).digest('hex')
const MAX_AGE = process.env.CACHE_MAX_AGE
const cacheControl = `max-age=${MAX_AGE}, must-revalidate`
const headers: { [key: string]: string } = {
ETag: freshETag,
'Cache-Control': cacheControl,
}
if (process.env.COMPRESS_RESPONSE === 'true') {
responseBody = zlib.deflateSync(responseBody).toString('base64')
headers['Content-Encoding'] = 'gzip, deflate'
}
return {
statusCode: 200,
body: responseBody,
headers,
}
} catch (error) {
console.error('Error fetching info:', error)
return {
statusCode: 500,
body: JSON.stringify({ message: 'Error fetching info: Internal Server Error' }),
}
}
}
Share
Improve this question
edited Mar 19 at 10:13
Estus Flask
224k79 gold badges472 silver badges611 bronze badges
asked Mar 18 at 20:52
AllanhAllanh
5152 gold badges8 silver badges20 bronze badges
1 Answer
Reset to default 0Return isBase64Encoded: true
when you body is encoded. So, the response will look like this:
// ...
return {
isBase64Encoded: true,
statusCode: 200,
body: responseBody,
headers,
}
// ...
本文标签:
版权声明:本文标题:node.js - how to compress response in nodeJS without using express and see results in postman naturally - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744491814a2608779.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论