Skip to content

Instantly share code, notes, and snippets.

@jcayzac
Created February 10, 2012 03:32
Show Gist options
  • Save jcayzac/1786196 to your computer and use it in GitHub Desktop.
Save jcayzac/1786196 to your computer and use it in GitHub Desktop.
var fs = require('fs'),
    connect = require('connect')

var oneYear = 31557600000,
    app = connect(
        connect.favicon(),
        connect.logger(),
        connect.staticCache(),
        connect.static(__dirname + '/static', { maxAge: oneYear }),
        connect.query(),
        connect.bodyParser(),
        connect.cookieParser(),
        connect.router(function(app){
            app.get('/test', function(req, res, next){
                res.setHeader("Content-Type", "text/plain")
                var user = req.connection.authorized ? req.connection.getPeerCertificate().subject.CN : 'anonymous coward';
                res.write('Hello ' + user + '!\n')
                res.end()
            });
        })
    )

delete connect.bodyParser.parse['multipart/form-data']

app.listen(80)

connect(
    {
        requestCert: true,
        rejectUnauthorized: false,
        key: fs.readFileSync(__dirname + '/certs/v2.julien.cayzac.name.key'),
        cert: fs.readFileSync(__dirname + '/certs/v2.julien.cayzac.name.crt'),
        ca: [fs.readFileSync(__dirname + '/certs/myca.crt')]
    },
    app
).listen(443)
$ curl http://v2.julien.cayzac.name/test
Hello anonymous coward!
$ curl https://v2.julien.cayzac.name/test
Hello anonymous coward!
$ curl --cert certs/client.crt --key certs/client.key https://v2.julien.cayzac.name/test
Hello [email protected]!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment