feat: Set up server using express

This commit is contained in:
Joachim Van Herwegen
2020-07-10 14:23:45 +02:00
parent c53ab5ed9b
commit a9dc59bf78
7 changed files with 1435 additions and 97 deletions

View File

@@ -0,0 +1,28 @@
import cors from 'cors';
import express from 'express';
import { HttpHandler } from './HttpHandler';
import { Server } from 'http';
export class ExpressHttpServer {
private readonly handler: HttpHandler;
public constructor(handler: HttpHandler) {
this.handler = handler;
}
public listen(port?: number): Server {
const app = express();
app.use(cors({
// Based on https://github.com/solid/solid-spec/blob/master/recommendations-server.md#cors---cross-origin-resource-sharing
// By default origin is always '*', this forces it to be the origin header if there is one
origin: (origin, callback): void => callback(null, (origin || '*') as any),
methods: [ 'GET', 'HEAD', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE' ],
}));
app.use(async(request, response): Promise<void> => {
await this.handler.handleSafe({ request, response });
});
return app.listen(port);
}
}