Methods

status(code)

Sets the status of the response that was returned to the Gateway API or ALB. By default, the response status is 200 and 500 for a general error.

Status 405 is used when the method does not exist and 404 when the route is not found.

For better control you can use the HttpStatus object which has a listing of all http codes that you can return.

Example:

router.get("/", (req, res) => {
  res.status(HttpStatus.OK).send(true);
});

header(key, value)

Use the header function if you want to add custom header that They will be returned to the Gateway API or ALB.

Remember that all keys will be converted to lowercase.

Example:

router.get("/", (req, res) => {
  res.header("X-Custom-Header", "my-custom-header");
  // return to "x-custom-header" in lowercase
  ...
})

cors(options)

Allows you to add custom cors for each request. If you want to work with the cors by default, use the static value cors.

Example:

router.get("/", (req, res) => {
  res.cors({
    credentials: true,
    origin: "https://mydomain.com",
  });
  ...
})

toResponse()

Returns the previous object that will be returned.

Structure:

interface Response {
  isBase64Encoded: boolean;
  statusCode: number;
  statusDescription?: string;
  body: string;
  headers: {
    [key: string]: string | undefined;
  };
}

toResponseError()

Returns the previous object that will be returned.

Structure:

export interface ResponseError {
  status?: HttpStatus;
  code: string;
  message: string;
  data?: {
    [key: string]: unknown;
  };
}

send(payload, isError)

Allows you to send a json object or string that will be transformed to a string that is the data that API Gateway or ALB expects to receive.

The parameter isError is optional is used only by the method error.

res.send("Hola Mundo");

// Return
{
  isBase64Encoded: false;
  statusCode: 200;
  body: "Hola Mundo";
  headers: {
    ...
  };
}

Or

res.send({
  id: 1,
  name: "admin",
  status: true
});

// Return
{
  isBase64Encoded: false;
  statusCode: 200;
  body: "{'user': 1, 'name': 'admin', 'status': true}";
  headers: {
    ...
  };
}

json(body)

A metode that defaults to the conten-type header in application/json And that receives as a parameter a JSON.

res.json({
  id: 1,
  name: "admin",
  status: true,
});

html(body)

A method that defaults to the conten-type header in text/html; charset=UTF-8 And that receives as a parameter a String.

res.json("<h1>Hello World</h1>");

file(body)

A metode that defaults to the conten-type header in tapplication/octet-stream And that receives as a parameter a String.

res.json("This is a file");

error(data)

A metode that receives as a parameter an object with a structure Predefined (ResponseError) that will be sent to the send method

Structure:

interface ResponseError {
  status?: HttpStatus;
  code: string;
  message: string;
  data?: {
    [key: string]: unknown;
  };
}

Remember that if you use the response object only, you must assign the status of the response.

Example:

res.status(400);
res.error(...);

You can use a throw to generate an exception in your route and be captured by the global error handler.

Example:

router.get("/", () => {
  throw Error("Unknown error");
});

// Return
{
  status: 500,
  code: "GENERIC_ERROR",
  message: "Unknown Error",
}

All errors extend from the HttpError class and this In turn of the generic class Error.

class HttpError extends Error {
  constructor(public code: string, public message: string) {
    super(message);
  }
}

Last updated

Was this helpful?