@aws-smithy/server-node
TypeScript icon, indicating that this package has built-in type declarations

1.0.0-alpha.10 • Public • Published

smithy-typescript/server-node

This package provides glue code to enable using a server sdk with NodeJS.

Usage

Example

import { createServer, IncomingMessage, ServerResponse } from "http";
import { HttpRequest } from "@aws-sdk/protocol-http";
import {
  GreetingService as __GreetingService,
  SayHelloInput,
  SayHelloOutput,
  getGreetingServiceHandler,
} from "@greeting-service/service-greeting";
import { convertEvent, convertResponse } from "@aws-smithy/server-node";
class GreetingService implements __GreetingService {
  SayHello(input: SayHelloInput, request: HttpRequest): SayHelloOutput {
    return {
      greeting: `Hello ${input.name}! How is ${input.city}?`,
    };
  }
}
const serviceHandler = getGreetingServiceHandler(new GreetingService());

const server = createServer(async function (
  req: IncomingMessage,
  res: ServerResponse<IncomingMessage> & { req: IncomingMessage }
) {
  // Convert NodeJS's http request to an HttpRequest.
  const httpRequest = convertRequest(req);

  // Call the service handler, which will route the request to the GreetingService
  // implementation and then serialize the response to an HttpResponse.
  const httpResponse = await serviceHandler.handle(httpRequest);

  // Write the HttpResponse to NodeJS http's response expected format.
  return writeResponse(httpResponse, res);
});

server.listen(3000);
console.error("Listening on port 3000");

Readme

Keywords

none

Package Sidebar

Install

npm i @aws-smithy/server-node

Weekly Downloads

414

Version

1.0.0-alpha.10

License

Apache-2.0

Unpacked Size

16.6 kB

Total Files

7

Last publish

Collaborators

  • pose
  • smithy-team
  • chasecoa
  • hpmellema
  • syall
  • mattsb42-aws
  • jordonphillips
  • amzn-oss
  • adamthom
  • gosar