On Github Floby / nodejsparis-express-hapi
for APIs with Node.js
Hello world, notes
Métriques Express Hapi Github stars 19k 4,2k Github fork 3,6k 0,6k StackOverflow 15k 180 Contributor 177 114 Github require ~360k 6k First Commit 26 Juin 2009 6 Août 2011
var express = require('express'); var app = express(); var MeetupDAO = require(somePath)(someConfiguration); app.get('/meetups', function (req, res) { MeetupDAO.all(function (err, meetups) { if(err) res.sendStatus(500); else res.json(meetups); }); }); // autres routes CRUD: collection.post, member.get,put,delete . var server = app.listen(3000, function () { var host = server.address().address; var port = server.address().port; console.log('Example app listening at http://%s:%s', host, port); });
var bodyParser = require('body-parser'); var morgan = require('morgan'); // loading app.use(bodyParser.json()); app.use(morgan('combined')); // declaration function logger(request, response, next) { var start = +new Date(); var url = request.url; var method = request.method; response.on('finish', function() { var duration = +new Date() - start; console.log(method + ' ' + url +' (' + duration + ')'); }); next(); }
app.route('/meetups') .get(MeetupController.all) .post(MeetupController.create); app.route('/meetups/:id') .get(MeetupController.get) .put(MeetupController.update) .patch(MeetupController.partialUpdate) .delete(MeetupController.remove);
function all(req, res) { MeetupDAO.all(function (all) { res.json(all); }); } function create(req, res) { if (!req.body.url) { return res.sendStatus(400); } var meetup = Meetup.create(req.body.url, req.body.title, req.body.date); MeetupDAO.save(meetup, function (newMeetup) { res.location(meetupsEndpoint + '/' + newMeetup._id); res.status(201).json({}); }); }
var StandardError = require('standard-error'); app.use(function (err, req, res, next) { if (err instanceof IKnowThisError) { res.status(503).json({ error: 'service_unavaible', reason: 'Looks like one of our partners is not doing his job' }); } else { next(err); } }); app.use(function (err, req, res, next) { if (err instanceof StandardError) { res.status(err.code); res.json(error.toJSON()); } else { next(err); } });
var MeetupDAO = require(somePath)(someConfiguration); var server = new Hapi.Server({ load: {sampleInterval: 5000}}); server.connection({port: 3000}); server.route([ { method: 'GET', path: '/meetups', handler: function (request, reply) { MeetupDAO.all(function (err, meetups) { if(err) reply().code(500); else reply(meetups); }); } } // autres routes. ]); server.start(function () { console.log('Server running at:', server.info.uri); });
var meetupRoutes = [ { method: 'GET', path: '/meetups', handler: MeetupController.all }, { method: 'POST', path: '/meetups', config: { handler: MeetupController.create, validate: {payload: schemas.meetupCreate} } }, { method: 'PUT', path: '/meetups/{id}', config: { handler: MeetupController.update, validate: { payload: schemas.meetupUpdate } } }, { method: 'DELETE', path: '/meetups/{id}', handler: MeetupController.remove }, { method: 'GET', path: '/meetups/{id}', handler: MeetupController.get }, ];
function all(request, reply) { MeetupDAO.all(function (all) { reply(all); }); } function create(request, reply) { if (!request.payload.url) { return reply().code(400); } var meetup = Meetup.create(request.payload.url, request.payload.title, request.payload.date); MeetupDAO.save(meetup, function (newMeetup) { reply().created(meetupsEndpoint + '/' + newMeetup._id); }); }
var Boom = require('boom'); //.... server.route({ method: 'GET', path: '/meetups/{id}', handler: function getMeetup(request, reply) { if (!request.params.id) { return reply(Boom.badRequest('No id was provided')); } MeetupDAO.get(request.params.id, function (err, meetup) { if(err) reply(Boom.wrap(err, 500)); else reply(meetup); }); } });
app.set('db', someDbConnection) // injecte 'db' dans 'app' app.use(bodyparse.json()); // injecte `.body` dans `req` app.use(cookieparser()); // injecte `.cookies` dans `req` app.use(session()); // injecte `.session` dans `req` app.use(function currentUser(req, res, next) { var userId = req.session.userId; if (!userId) return next(); app.get('db').users.findOne({_id: userId}, function (err, user) { if (err) return next(err); req.currentUser = user // injection de l'utilsateur courant }); });
server.bind({db: someDbConnection}); server.register({ // injecte `.session` sur l'objet `.request` register: require('yar') }) // ... { method: 'GET', path: '/current-user', handler: function (request, reply) { this.db.users.findOne({_id: request.session.userId}, function (err, user) { if (err) return reply(err); reply(user); }) } }
have some more links
Pourquoi utiliser Node pour réaliser mon API ?