Last active
February 16, 2016 08:21
-
-
Save lsongdev/60346516233295cad3f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const connect = require('connect'); | |
const route = require('connect-routeify'); | |
const bodyParser = require('body-parser'); | |
const app = connect(); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: true })); | |
app.use(function(req, res, next){ | |
var start = new Date; | |
next(); | |
console.log('-> %s %s %s %sms', | |
req.method, | |
req.url, | |
res.statusCode, | |
new Date - start); | |
}); | |
app.use(function(req, res, next){ | |
res.status = function(status){ | |
res.writeHead(status); | |
return res; | |
}; | |
res.send = function(body){ | |
res.end(typeof body == 'string' ? body : JSON.stringify(body)); | |
return res; | |
}; | |
next(); | |
}); | |
app.use(route('put', '/-/user/org.couchdb.user::username', function(req, res, next){ | |
var user = req.body; | |
user.token = new Buffer([user.name, user.password].join(':')).toString('base64'); | |
res.status(201).send(user); | |
})); | |
app.use(route('get', '/-/whoami', function(req, res, next){ | |
console.log(req.headers[ 'authorization' ]); | |
res.send({ | |
username: 'aa' | |
}); | |
})); | |
app.use(route('get', '/:package', function(req, res, next){ | |
res.send({ | |
name: 'a', | |
'dist-tags': { | |
latest: '1.1.1' | |
}, | |
versions: { | |
'1.1.1': { | |
name: 'a', | |
version: '1.1.1', | |
dist: { | |
shasum: 'f84bc93da34e294c573386eb63083904b0132e4b', | |
tarball: 'http://registry.npmjs.org/input-event/-/input-event-1.0.0.tgz' | |
} | |
} | |
} | |
}); | |
})); | |
app.use(route('put', '/:package', function(req, res, next){ | |
console.log(req.params[ 'package' ]) | |
res.send({}); | |
})); | |
app.listen(4000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment