Skip to content

Instantly share code, notes, and snippets.

@mcavage
Created February 9, 2012 18:39
Show Gist options
  • Save mcavage/1781903 to your computer and use it in GitHub Desktop.
Save mcavage/1781903 to your computer and use it in GitHub Desktop.
var restify = require('restify');
///--- Globals
var NotAuthorizedError = restify.NotAuthorizedError;
///--- Helpers
function authenticate(req, res, next) {
var authz = req.authorization;
console.log('%j', authz)
if (authz.scheme !== 'Basic' ||
authz.basic.username !== 'root' ||
authz.basic.password !== 'secret') {
return next(new NotAuthorizedError('%s failed to authenticate',
req.authorization.username));
}
return next();
}
///--- Mainline
var server = restify.createServer();
server.use(restify.authorizationParser());
server.use(authenticate);
server.get('/hello/:name', function(req, res, next) {
res.send(req.params.name);
return next();
});
server.listen(8080, function() {
console.log('server listening at %s', server.url);
console.log('\nTry:\n curl -is localhost:8080/hello/jerry');
console.log('\nAnd:\n curl -is -u root:secret localhost:8080/hello/jerry');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment