18.05.2016 Views

Mittwoch, 18. Mai, 2016

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Finishing Angular TODO application and deploying to production 115<br />

95 next();<br />

96 };<br />

This, rather nicely structured, mumbo jumbo explained per functions:<br />

• getErrorMessage - iterates over the Mongoose errors and returns the first one<br />

• create<br />

– creates a new Todo model instance using the HTTP request body<br />

– adds the authenticated Passport user as the todo creator<br />

– saves the todo document using Mongoose instance save method<br />

• list<br />

– fetches the collection of todo documents by using the Mongoose instance find method<br />

– sorts them by created property<br />

– adds name and _username_fields to to the creator property<br />

• read - outputs the todo object as JSON<br />

• todoById- fetches the todo document by using the Mongoose instance findById method.<br />

• update - updates the todo document which you’ve previously fetched with the todoById<br />

middleware (if you remeber middlewares that is, if not, please go and refresh your memory<br />

in the second tutorial which was all about Node.js and Express²⁰²)<br />

• delete - deletes the todo document which you’ve previously fetched with the todoById<br />

middleware<br />

• hasAuthorization - middleware that uses the req.todo and req.user objects to verify that the<br />

current user is the creator of the current todo.<br />

To continue you need to say the magic word<br />

You need to know which user is authenticated so that you know which one made a certain action<br />

(like for example created a new todo). You can check this inside every method, but that’s just code<br />

duplication since you can use the Express middleware chaining to block unauthorized requests from<br />

executing your controller methods.<br />

So, in the app/controllers/users.server.controller.js file add the following code:<br />

²⁰²https://hackhands.com/delving-node-js-express-web-framework/

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!