Added error handling to web-service example
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent fda31b7 · Browse files · 1 file·+54 added−25 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+54 added−25 removed
- other:+54 added−25 removed
Provenance
- Commit
e4342a7- Tree
2374f4a- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
fda31b7
1 file changed+54 added−25 removed
modified:examples/web-service/app.js+54 added-25 removed
5 unmodified lines6789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616234 unmodified lines97989910052535455565758596061626364656667686970717273741011021031044 unmodified lines10911011111287881131141151161171181191201215 unmodified linesvar express = require('../../lib/express');
var app = express.createServer();
// configuration
// if we wanted to supply more than JSON, we could// use something similar to the content-negotiation// example.
// here we validate the API key,// by mounting this middleware to /api/v1// meaning only paths prefixed with "/api/v1"// will cause this middleware to be invoked
app.use('/api/v1', function(req, res, next){ var key = req.query['api-key'];
// key isnt present if (!key) return next(new Error('api key required'));
// key is invalid if (!~apiKeys.indexOf(key)) return next(new Error('invalid api key'));
// all good, store req.key for route access req.key = key; next();});
// position our routes above the error handling middleware,// and below our API middleware, since we want the API validation// to take place BEFORE our routesapp.use(app.router);
// middleware with an arity of 4 are considered// error handling middleware. When you next(err)// it will be passed through the defined middleware// in order, but ONLY those with an arity of 4, ignoring// regular middleware.app.use(function(err, req, res, next){ // whatever you want here, feel free to populate // properties on `err` to treat it differently in here, // or when you next(err) set res.statusCode= etc. res.send({ error: err.message }, 500);});
// our custom JSON 404 middleware. Since it's placed last// it will be the last middleware called, if all others// invoke next() and do not respond.app.use(function(req, res){ res.send({ error: "Lame, can't find that" }, 404);});
/** * Generate our unique identifier. */
34 unmodified lines , loki: [repos[1]] , jane: [repos[2]]};
// if we wanted to supply more than JSON, we could// use something similar to the content-negotiation// example.
// here we validate the API key,// by mounting this middleware to /api/v1// meaning only paths prefixed with "/api/v1"// will cause this middleware to be invoked
app.use('/api/v1', function(req, res, next){ var key = req.query['api-key'];
// key isnt present if (!key) return res.send('api key required', 401);
// key is invalid if (!~apiKeys.indexOf(key)) return res.send('invalid api key', 401);
// all good, store req.key for route access req.key = key; next();});
// we now can assume the api key is valid,// and simply expose the data
app.get('/api/v1/users', function(req, res, next){4 unmodified lines res.send(repos);});
app.get('/api/v1/user/:name/repos', function(req, res, next){ var name = req.params.name; res.send(userRepos[name]); var name = req.params.name , user = userRepos[name]; if (user) res.send(user); else next();});
app.listen(3000);console.log('Express server listening on port 3000');No newline at end of fileNo newline at end of file