project barebone (?)
This commit is contained in:
parent
dee4768c7e
commit
958e0134a0
|
@ -0,0 +1,2 @@
|
|||
/node_modules
|
||||
*.js
|
|
@ -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);
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"noImplicitAny": true,
|
||||
"removeComments": true,
|
||||
"moduleResolution": "node",
|
||||
"target": "es6"
|
||||
},
|
||||
"files": [
|
||||
"mixer.ts",
|
||||
"main.ts"
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue