mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Set up server using express
This commit is contained in:
28
src/server/ExpressHttpServer.ts
Normal file
28
src/server/ExpressHttpServer.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user