ufront / ufront-mvc

The core MVC framework that powers ufront
MIT License
17 stars 15 forks source link

Support https on Node.js #34

Closed kevinresol closed 6 years ago

kevinresol commented 8 years ago

Here is the code I used, but I am not sure how to integrate it into the core classes

//Server.hx
var ufrontApp = new MyApplication(...);
var port = #if !ssl 2987 #else 443 #end;
ufrontApp.listen(port); 

class MyApplication extends UfrontApplication
{
    // I defined 'ssl' in my hxml
    #if (nodejs && ssl)
    override public function listen(?port:Int=2987):Void
    {
        var app = new express.Express();
        app.use( express.Express.serveStatic(".") );
        app.use( mw.BodyParser.json({limit:'1500kb'}) );
        app.use( mw.BodyParser.urlencoded({ extended: true, limit:'1500kb'}) );
        // TODO: check if we need to use a mw.BodyParser() middleware here.
        var ufAppMiddleware:express.Middleware = function(req:express.Request,res:express.Response,next:express.Error->Void) {
            var context:HttpContext =
                if ( pathToContentDir!=null ) HttpContext.createNodeJsContext( req, res, injector, urlFilters, pathToContentDir )
                else HttpContext.createNodeJsContext( req, res, urlFilters );
            this.execute( context ).handle( function(result) switch result {
                case Failure( err ): next( new express.Error(err.toString()) );
                default: next( null );
            });
        };
        app.use( ufAppMiddleware );

        var httpsOptions = {
            key: js.node.Fs.readFileSync('/path/to/my.key').toString(),
            cert: js.node.Fs.readFileSync('/path/to/my.crt').toString(),
        }
        js.node.Https.createServer(httpsOptions, untyped app).listen(port);

        // TODO: redirect http -> https
    }
    #end
}
jasononeil commented 8 years ago

Thanks for this. The sample code is useful to see how it works (and help other people who need to use https with node now).

I'll have to take a look at the best way to integrate this as a "standard" option.

kevinresol commented 6 years ago

Cleaning up my old issues and I think this is no longer needed.