index.js
This file
Path
examples/web-service/index.js
112 lines · 2.8 KiB · JavaScript
Last changed by
docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
History of this fileTake it
The walk stopped after 200 commits: lines older than 6bc7574 point at it.
docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 1–112why, one step back1/**2 * Module dependencies.3 */45var express = require('../../');67var app = module.exports = express();89// create an error with .status. we10// can then use the property in our11// custom error handler (Connect repects this prop as well)1213function error(status, msg) {14 var err = new Error(msg);15 err.status = status;16 return err;17}1819// if we wanted to supply more than JSON, we could20// use something similar to the content-negotiation21// example.2223// here we validate the API key,24// by mounting this middleware to /api25// meaning only paths prefixed with "/api"26// will cause this middleware to be invoked2728app.use('/api', function(req, res, next){29 var key = req.query['api-key'];3031 // key isn't present32 if (!key) return next(error(400, 'api key required'));3334 // key is invalid35 if (!~apiKeys.indexOf(key)) return next(error(401, 'invalid api key'));3637 // all good, store req.key for route access38 req.key = key;39 next();40});4142// map of valid api keys, typically mapped to43// account info with some sort of database like redis.44// api keys do _not_ serve as authentication, merely to45// track API usage or help prevent malicious behavior etc.4647var apiKeys = ['foo', 'bar', 'baz'];4849// these two objects will serve as our faux database5051var repos = [52 { name: 'express', url: 'http://github.com/strongloop/express' }53 , { name: 'stylus', url: 'http://github.com/learnboost/stylus' }54 , { name: 'cluster', url: 'http://github.com/learnboost/cluster' }55];5657var users = [58 { name: 'tobi' }59 , { name: 'loki' }60 , { name: 'jane' }61];6263var userRepos = {64 tobi: [repos[0], repos[1]]65 , loki: [repos[1]]66 , jane: [repos[2]]67};6869// we now can assume the api key is valid,70// and simply expose the data7172app.get('/api/users', function(req, res, next){73 res.send(users);74});7576app.get('/api/repos', function(req, res, next){77 res.send(repos);78});7980app.get('/api/user/:name/repos', function(req, res, next){81 var name = req.params.name;82 var user = userRepos[name];8384 if (user) res.send(user);85 else next();86});8788// middleware with an arity of 4 are considered89// error handling middleware. When you next(err)90// it will be passed through the defined middleware91// in order, but ONLY those with an arity of 4, ignoring92// regular middleware.93app.use(function(err, req, res, next){94 // whatever you want here, feel free to populate95 // properties on `err` to treat it differently in here.96 res.status(err.status || 500);97 res.send({ error: err.message });98});99100// our custom JSON 404 middleware. Since it's placed last101// it will be the last middleware called, if all others102// invoke next() and do not respond.103app.use(function(req, res){104 res.status(404);105 res.send({ error: "Lame, can't find that" });106});107108/* istanbul ignore next */109if (!module.parent) {110 app.listen(3000);111 console.log('Express started on port 3000');112}