Express: add custom bind address setting

This commit is contained in:
Alice Gaudon 2020-09-17 21:15:37 +02:00
parent 9f31e5cbe9
commit dfee2645e1
4 changed files with 14 additions and 9 deletions

View File

@ -7,6 +7,7 @@ module.exports = {
db_log_level: "ERROR",
base_url: "http://localhost:4899",
public_websocket_url: "ws://localhost:4899",
listen_addr: '127.0.0.1',
port: 4899,
gitlab_webhook_token: 'default',
mysql: {

View File

@ -5,18 +5,20 @@ import {Server} from "http";
import compression from "compression";
export default class ExpressAppComponent extends ApplicationComponent<void> {
private readonly addr: string;
private readonly port: number;
private server?: Server;
private expressApp?: Express;
constructor(port: number) {
public constructor(addr: string, port: number) {
super();
this.addr = addr;
this.port = port;
}
public async start(app: Express): Promise<void> {
this.server = app.listen(this.port, 'localhost', () => {
Logger.info(`Web server running on localhost:${this.port}.`);
this.server = app.listen(this.port, this.addr, () => {
Logger.info(`Web server running on ${this.addr}:${this.port}.`);
});
// Proxy

View File

@ -4,7 +4,7 @@ import CsrfProtectionComponent from "../src/components/CsrfProtectionComponent";
import supertest from "supertest";
let app: TestApp;
useApp(port => {
useApp((addr, port) => {
return app = new class extends TestApp {
protected async init(): Promise<void> {
this.use(new class extends Controller {
@ -29,7 +29,7 @@ useApp(port => {
super.registerComponents();
this.use(new CsrfProtectionComponent());
}
}(port);
}(addr, port);
});
describe('Test CSRF protection', () => {

View File

@ -18,12 +18,12 @@ import RedirectBackComponent from "../src/components/RedirectBackComponent";
import ServeStaticDirectoryComponent from "../src/components/ServeStaticDirectoryComponent";
import {Express} from "express";
export default function useApp(appSupplier?: (port: number) => TestApp) {
export default function useApp(appSupplier?: (addr: string, port: number) => TestApp) {
let app: Application;
beforeAll(async (done) => {
await setupMailServer();
app = appSupplier ? appSupplier(8966) : new TestApp(8966);
app = appSupplier ? appSupplier('127.0.0.1', 8966) : new TestApp('127.0.0.1', 8966);
await app.start();
done();
@ -37,11 +37,13 @@ export default function useApp(appSupplier?: (port: number) => TestApp) {
}
export class TestApp extends Application {
private readonly addr: string;
private readonly port: number;
private expressAppComponent?: ExpressAppComponent;
constructor(port: number) {
constructor(addr: string, port: number) {
super(require('../package.json').version, true);
this.addr = addr;
this.port = port;
}
@ -56,7 +58,7 @@ export class TestApp extends Application {
}
protected registerComponents() {
this.expressAppComponent = new ExpressAppComponent(this.port);
this.expressAppComponent = new ExpressAppComponent(this.addr, this.port);
const redisComponent = new RedisComponent();
const mysqlComponent = new MysqlComponent();