Inversifyjs: There is no way to create a HTTPS/TLS server

Created on 11 Sep 2017  路  4Comments  路  Source: inversify/InversifyJS

I did not find in the documentation how to do this. If it is still possible, how to do it?

Most helpful comment

Hey thanks for the issue, I must say that I never that of that issue.

I think it could work with something like this: (not tested!)

import * as bodyParser from 'body-parser';
import * as fs from 'fs';
import * as http from 'http';
import * as https from 'https';

import { Container } from 'inversify';
import { interfaces, InversifyExpressServer, TYPE } from 'inversify-express-utils';

// replace this with your private key
const privateKey  = fs.readFileSync('sslcert/server.key', 'utf8');
// replace this with you certificate
const certificate = fs.readFileSync('sslcert/server.crt', 'utf8');
const credentials = {key: privateKey, cert: certificate};

// set up container
let container = new Container();

// note that you *must* bind your controllers to Controller
container.bind<interfaces.Controller>(TYPE.Controller).to(FooController).whenTargetNamed('FooController');
container.bind<FooService>('FooService').to(FooService);

// create server
let server = new InversifyExpressServer(container);
server.setConfig((app) => {
  // config for express
  app.use(bodyParser.urlencoded({
    extended: true
  }));
  app.use(bodyParser.json());
});

let app = server.build();

// create the http server
const httpServer = http.createServer(app);
// create the https server
const httpsServer = https.createServer(credentials, app);

httpServer.listen(8080);
httpsServer.listen(8443);

Please let me know if this helps :)
Have a nice day.

All 4 comments

Hey thanks for the issue, I must say that I never that of that issue.

I think it could work with something like this: (not tested!)

import * as bodyParser from 'body-parser';
import * as fs from 'fs';
import * as http from 'http';
import * as https from 'https';

import { Container } from 'inversify';
import { interfaces, InversifyExpressServer, TYPE } from 'inversify-express-utils';

// replace this with your private key
const privateKey  = fs.readFileSync('sslcert/server.key', 'utf8');
// replace this with you certificate
const certificate = fs.readFileSync('sslcert/server.crt', 'utf8');
const credentials = {key: privateKey, cert: certificate};

// set up container
let container = new Container();

// note that you *must* bind your controllers to Controller
container.bind<interfaces.Controller>(TYPE.Controller).to(FooController).whenTargetNamed('FooController');
container.bind<FooService>('FooService').to(FooService);

// create server
let server = new InversifyExpressServer(container);
server.setConfig((app) => {
  // config for express
  app.use(bodyParser.urlencoded({
    extended: true
  }));
  app.use(bodyParser.json());
});

let app = server.build();

// create the http server
const httpServer = http.createServer(app);
// create the https server
const httpsServer = https.createServer(credentials, app);

httpServer.listen(8080);
httpsServer.listen(8443);

Please let me know if this helps :)
Have a nice day.

@lholznagel Thanks, this works great!

@lholznagel Is there a way to create HttpServer without using inversify-express-utils ?

@esakkikrishnan if you only want to use expressjs remove all the inversify-express-utils and replace let app = server.build(); with let app = express()

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AriaFallah picture AriaFallah  路  4Comments

jshearer picture jshearer  路  4Comments

stjepangolemac picture stjepangolemac  路  5Comments

remojansen picture remojansen  路  3Comments

codyjs picture codyjs  路  3Comments