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
Add a comment  | 

1 Answer 1

Reset to default 0

Return isBase64Encoded: true when you body is encoded. So, the response will look like this:

// ...
    return {
      isBase64Encoded: true,
      statusCode: 200,
      body: responseBody,
      headers,
    }
// ...

本文标签: