Key Takeaways
He uses UMD style modules to share code between browser and server.
He loads modules on the server with require:
global.Validate = Validate = require(path.join(__dirname,"web","js","Validate.js"));
A detail: he always put the shared modules on a path (/web/js/Validate.js) where he could access them in both contexts. In a real production app, you would be building (packaging) and moving the files to another location.
To set up routes, he pushes functions that first check the
req.url
.
function FooRoute(req,res) {
if (/^\/Foo/.test(req.url)) {
// ...
There are some libraries that help with these regexes etc.If a route function can handle a request, it
return true
, otherwise it falls to the next route and finally to defaultRoute
. He again emphasises that you don't need big, complex frameworks.