Skip to content

Instantly share code, notes, and snippets.

@dominikzogg
Created August 31, 2021 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dominikzogg/600cddb2a6fc183902f19d0b946255a3 to your computer and use it in GitHub Desktop.
Save dominikzogg/600cddb2a6fc183902f19d0b946255a3 to your computer and use it in GitHub Desktop.
server.ts
import ServerRequestFactory from '@chubbyjs/chubbyjs-http-message/dist/Factory/ServerRequestFactory';
import ResponseFactory from '@chubbyjs/chubbyjs-http-message/dist/Factory/ResponseFactory';
import UriFactory from '@chubbyjs/chubbyjs-http-message/dist/Factory/UriFactory';
import { createServer } from 'http';
import PsrRequestFactory from './PsrRequestFactory';
import PsrResponseEmitter from './PsrResponseEmitter';
import ServerRequestInterface from '@chubbyjs/psr-http-message/dist/ServerRequestInterface';
import ResponseInterface from '@chubbyjs/psr-http-message/dist/ResponseInterface';
const responseFactory = new ResponseFactory();
const psrRequestFactory = new PsrRequestFactory(new ServerRequestFactory(), new UriFactory());
const psrResponseEmitter = new PsrResponseEmitter();
const handler = (serverRequest: ServerRequestInterface): ResponseInterface => {
console.log(serverRequest);
const response = responseFactory.createResponse(200);
response.getBody().write('Hello, World');
response.getBody().end();
return response;
};
const server = createServer(function (req, res) {
const serverRequest = psrRequestFactory.create(req);
const response = handler(serverRequest);
psrResponseEmitter.emit(response, res);
});
server.listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment