[backend][nodejs] Use express framework instead of http module

This commit is contained in:
koeberlue
2021-04-05 21:40:17 +02:00
parent 6586d07745
commit 148c283a91
2 changed files with 9 additions and 11 deletions

View File

@@ -5,7 +5,7 @@
"main": "lib/app.js", "main": "lib/app.js",
"scripts": { "scripts": {
"test": "ts-mocha -p tsconfig.json src/**/*.spec.ts", "test": "ts-mocha -p tsconfig.json src/**/*.spec.ts",
"start": "tsc && node lib/app.js build:live", "start": "tsc && node lib/app.js",
"build": "tsc -p .", "build": "tsc -p .",
"build:live": "nodemon --watch 'src/**/*.ts' --exec \"ts-node\" src/app.ts" "build:live": "nodemon --watch 'src/**/*.ts' --exec \"ts-node\" src/app.ts"
}, },

View File

@@ -1,15 +1,13 @@
import {IncomingMessage, ServerResponse} from "http"; import express from 'express';
const http = require('http');
const randomDecision = require("./random-decision") const randomDecision = require("./random-decision")
const app = express();
const port = 3000; const port = 3000;
const server = http.createServer((request: IncomingMessage, response: ServerResponse) => { app.get('/', ((req, response) => {
response.statusCode = 200; response.send(randomDecision.getRandomChoice());
response.setHeader('Content-Type', 'text/plain'); }));
response.end(randomDecision.getRandomChoice().toString());
});
server.listen(port, () => { app.listen(port, () => {
console.log(`Server listening on port ${port}`) return console.log(`Server is listening on port ${port}`);
}); });