Created
August 31, 2021 19:40
-
-
Save dominikzogg/600cddb2a6fc183902f19d0b946255a3 to your computer and use it in GitHub Desktop.
server.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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