project barebone (?)

This commit is contained in:
Adrien Marquès 2018-10-17 17:34:36 +02:00
parent dee4768c7e
commit 958e0134a0
4 changed files with 57 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/node_modules
*.js

18
main.ts Normal file
View File

@ -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);

24
mixer.ts Normal file
View File

@ -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();
}
}

13
tsconfig.json Normal file
View File

@ -0,0 +1,13 @@
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"moduleResolution": "node",
"target": "es6"
},
"files": [
"mixer.ts",
"main.ts"
]
}