This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends main.html %} | |
{% block content %} | |
{% if (test) %} | |
truthy eval | |
{% else %} | |
falsy eval | |
{% endif %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>{{ title }}</title> | |
</head> | |
<body> | |
<div> | |
{% block content %} | |
this is the content of a block, it can be overriden. | |
{% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
router.get('/chain/example', [ | |
(req, res, next) => { | |
req.chain = 'one ->' | |
next() | |
}, | |
(req, res, next) => { | |
req.chain = `${ req.chain} two ->` | |
next() | |
}, | |
(req, res, next) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Router { | |
... | |
_dispatch(request, response){ | |
let match, handledRequest = false, {url} = request | |
for(let route of this._routes){ | |
// 1- Keep trying till we find a match | |
match = url.match(route.regex) | |
if(match === null || !route.methods.includes(request.method)){ | |
continue |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Router { | |
... | |
_patternToRegex(pattern){ | |
let regex = '' | |
let params = [] | |
let parts = pattern.split('/') | |
for(let part of parts){ | |
if(part.trim() === '') | |
continue |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Router { | |
... | |
route(specs, methods, handlers){ | |
if(this._prefix !== ''){ | |
specs = `${this._prefix}/${specs}` | |
} | |
handlers = this._cleanHandlers(handlers) | |
this._routes.push({ | |
methods, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Router { | |
constructor(){ | |
this._routes = [] | |
this._prefix = '' | |
} | |
route(specs, methods, handlers){ ... } | |
group(prefix, fn){ ... } | |
all(specs, handlers){ ... } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_handleRequest(request, response){ | |
// typeof(response.render) -> undefined | |
this._patchResponse(response) | |
// typeof(response.render) -> function | |
this.router._dispatch(request, response) | |
} | |
_patchResponse(response){ | |
response.render = (file, context) => { | |
try { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
route(fn){ | |
if( typeof(fn) !== 'function'){ | |
return | |
} | |
return fn(this.router) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
boot(callback){ | |
this._httpServer.listen(this.config.port, (err) => { | |
if(callback && typeof(callback) === 'function'){ | |
callback(err) | |
} | |
if (err) { | |
return console.log('Micro was not able to start correctly', err) | |
} | |
console.log(`Micro server is listening on ${this.config.port}`) | |
}) |