Notes and code from Day 2 lecture - DM16
- array of objects
var books = [{
name: ‘Harry Potter’,
author: ‘JK Rowling’
}, {
name: ‘LOTR’,
author: ‘JRR Tolkein’
}, {
name: ‘GOT’,
author: ‘George RR Martin’
}];
- other examples: movies, schools, users
// 1. module.exports at the top and have function declarations
module.exports = {
getBooks: getBooks,
updateBooks: updateBooks,
deleteBook: deleteBook
}
// 2. module.exports and write in the object itself
module.exports = {
create: function (req, res, next) {
},
read: function (req, res, next) {
},
update: function (req, res, next) {
},
destroy: function (req, res, next) {
},
index: function (req, res, next) {
}
}
// 3. module.exports at the bottom and use function declarations OR expressions
// 4. module.exports.functionName after every function
- just like Angular
- ‘/books/:id’ where :id is your req.params.id
- anything after the ‘?’ in the url
- key=value format. spaces get a ‘+’
- different queries are separated with ‘&’
- ‘/books?id=0&name=harry+potter’
req.query = { id: 0, name: ‘harry potter’ }
tip: if you’re not sure, console.log()
- similar to Angular where you divide up code by functionality
- use module.exports to export out code from a file
- use var whateverName = require(‘/path/to/file/with/code/we/want’)
- just be consistent and descriptive
- express does things in order
- you need to call next() to get to the next function in the chain
- if you use
app.use
then this will run for every single request - if you want to only use it for some some requests, then make a function, and then add it to specific endpoints
app.use(iAmUsedForEveryRequest());
function onlyUsedWhenIWantMiddleware (req, res, next) {
// do stuff
next()
// if you don’t call next it won’t continue
}
app.get(‘/books’, onlyUsedWhenIWantMiddleware, function(req, res, next) {
// stuff
});