const router = express.Router()
. A good practice is to modularize the routing then use it as a middleware with app.use('/', routes)
or the shorthand app.use(routes)
app.use
(or router.use
). They can be defined inline, but for readability purposes a good practice is to have middlewares definitions grouped either at the top or bottom (thanks to functions hoisting) of the file, or even in an external middlewares file (following the node modular architecture idea) next(err)
will actually stop the middleware flow and call the error-handling function, passing the error to it util.inspect()
can be used to debug objects in the console, otherwise node will simply return a [object OBJECT] instead of the object' string representation.__dirname
should always be used to construct dir names, instead of ./
so as to make sure the constructed dir is valid If module.exports is a single function, then it can either be called later on using the var assigned to its require, ex
let express = require('express')
let app = express()
But it can also be called on-place :
let app = require('express')()
Both examples produce the same result, making app() a new instance of the express function
NOTE FOR EXPRESS MIDDLEWARES
use()
function, there is no need to actually call them as express will do it for us.Chai makes used of chained methods for its assertions, e.g:
expect(varToTest).to.be.true
assert(varToTest, resultWanted)
Note: This is syntaxic sugar over node's native assert lib try/catch
or by chaining a .catch to the await(somePromise)
.await
can only be used in an async function. Which makes it unusable from the very top-level of a module, except if we wrap-it in a C-style main()
super(args)
in their constructor, allowing them to use their parent methods and vars Bluebird can be used the exact same way as the native ES6 promises, as such it can (and should) replace the native Promise constructor using require. However it has a few advantages:
Promise.prototype.finally
is currently in stage 3 of the process. And a lot more !
arr.indexOf(elementToCheck) === -1
: Checks for the presence of an element in an array_id
property, as findById method won't work anymore thenquery.exec()
will make sure that mongoose returns a promise that resolves to the result of our query. This is extremely useful : knowing this, we can create CRUD functions with ES7 that are very clean and concise, ex
async find(product) => {
return await ProductModel.find({product}).exec()
}
Using ES6 arrow functions, ES6 shorthand for {object : object}, ES7 async/await and mongoose await, this makes for very clean code !