Skip to content
Myr

index.js

This file

Path

examples/web-service/index.js

115 lines · 2.9 KiB · JavaScript

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than 8ac3e80 point at it.

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80

lines 1–115why, one step back
1
2/**3 * Module dependencies.4 */56var express = require('../../');78var app = module.exports = express();910// create an error with .status. we11// can then use the property in our12// custom error handler (Connect repects this prop as well)1314function error(status, msg) {15 var err = new Error(msg);16 err.status = status;17 return err;18}1920// if we wanted to supply more than JSON, we could21// use something similar to the content-negotiation22// example.2324// here we validate the API key,25// by mounting this middleware to /api26// meaning only paths prefixed with "/api"27// will cause this middleware to be invoked2829app.use('/api', function(req, res, next){30 var key = req.query['api-key'];3132 // key isnt present33 if (!key) return next(error(400, 'api key required'));3435 // key is invalid36 if (!~apiKeys.indexOf(key)) return next(error(401, 'invalid api key'));3738 // all good, store req.key for route access39 req.key = key;40 next();41});4243// position our routes above the error handling middleware,44// and below our API middleware, since we want the API validation45// to take place BEFORE our routes46app.use(app.router);4748// middleware with an arity of 4 are considered49// error handling middleware. When you next(err)50// it will be passed through the defined middleware51// in order, but ONLY those with an arity of 4, ignoring52// regular middleware.53app.use(function(err, req, res, next){54 // whatever you want here, feel free to populate55 // properties on `err` to treat it differently in here.56 res.send(err.status || 500, { error: err.message });57});5859// our custom JSON 404 middleware. Since it's placed last60// it will be the last middleware called, if all others61// invoke next() and do not respond.62app.use(function(req, res){63 res.send(404, { error: "Lame, can't find that" });64});6566// map of valid api keys, typically mapped to67// account info with some sort of database like redis.68// api keys do _not_ serve as authentication, merely to69// track API usage or help prevent malicious behavior etc.7071var apiKeys = ['foo', 'bar', 'baz'];7273// these two objects will serve as our faux database7475var repos = [76 { name: 'express', url: 'http://github.com/visionmedia/express' }77 , { name: 'stylus', url: 'http://github.com/learnboost/stylus' }78 , { name: 'cluster', url: 'http://github.com/learnboost/cluster' }79];8081var users = [82 { name: 'tobi' }83 , { name: 'loki' }84 , { name: 'jane' }85];8687var userRepos = {88 tobi: [repos[0], repos[1]]89 , loki: [repos[1]]90 , jane: [repos[2]]91};9293// we now can assume the api key is valid,94// and simply expose the data9596app.get('/api/users', function(req, res, next){97 res.send(users);98});99100app.get('/api/repos', function(req, res, next){101 res.send(repos);102});103104app.get('/api/user/:name/repos', function(req, res, next){105 var name = req.params.name106 , user = userRepos[name];107 108 if (user) res.send(user);109 else next();110});111112if (!module.parent) {113 app.listen(3000);114 console.log('Express server listening on port 3000');115}