diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fce56e0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/node_modules +*.js \ No newline at end of file diff --git a/main.ts b/main.ts new file mode 100644 index 0000000..0106471 --- /dev/null +++ b/main.ts @@ -0,0 +1,18 @@ +import { createServer } from 'http' +import { Socket } from 'net'; +import Mixer from './mixer' + + +const mixer: Mixer = new Mixer(); + + +// 1. build server +const server = createServer(mixer.http_handler); + +// 2. bind 404 error +server.on('clientError', (err: Error, sock: Socket) => { + sock.end('HTTP/1.1 400 Bad Request\r\n\r\n'); +}); + +// 3. listen on given port +server.listen(8000); diff --git a/mixer.ts b/mixer.ts new file mode 100644 index 0000000..031cc69 --- /dev/null +++ b/mixer.ts @@ -0,0 +1,24 @@ +import { IncomingMessage, ServerResponse, Server } from "http"; + +export default class Mixer { + + constructor(){ + console.log("mixer created"); + } + + http_handler(req: IncomingMessage, res: ServerResponse){ + + // reject invalid requests + if( req.method != 'POST' ){ + res.writeHead(200, { 'Content-Type': 'application/json' }) + res.end(JSON.stringify({reason: 'invalid HTTP method; only POST allowed'})) + }else if( req.url != "/" ){ + res.writeHead(200, { 'Content-Type': 'application/json' }) + res.end(JSON.stringify({reason: 'invalid URI; only / allowed'})) + } + + + res.end(); + } + +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..bc7db83 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "module": "commonjs", + "noImplicitAny": true, + "removeComments": true, + "moduleResolution": "node", + "target": "es6" + }, + "files": [ + "mixer.ts", + "main.ts" + ] +} \ No newline at end of file