Skip to content

Instantly share code, notes, and snippets.

View evanxg852000's full-sized avatar
πŸ’­
Awesomeness πŸ‘Œ

Evance Soumaoro evanxg852000

πŸ’­
Awesomeness πŸ‘Œ
View GitHub Profile
route(fn){
if( typeof(fn) !== 'function'){
return
}
return fn(this.router)
}
_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 {
class Router {
constructor(){
this._routes = []
this._prefix = ''
}
route(specs, methods, handlers){ ... }
group(prefix, fn){ ... }
all(specs, handlers){ ... }
class Router {
...
route(specs, methods, handlers){
if(this._prefix !== ''){
specs = `${this._prefix}/${specs}`
}
handlers = this._cleanHandlers(handlers)
this._routes.push({
methods,
class Router {
...
_patternToRegex(pattern){
let regex = ''
let params = []
let parts = pattern.split('/')
for(let part of parts){
if(part.trim() === '')
continue
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
router.get('/chain/example', [
(req, res, next) => {
req.chain = 'one ->'
next()
},
(req, res, next) => {
req.chain = `${ req.chain} two ->`
next()
},
(req, res, next) => {
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
<div>
{% block content %}
this is the content of a block, it can be overriden.
{% endblock %}
{% extends main.html %}
{% block content %}
{% if (test) %}
truthy eval
{% else %}
falsy eval
{% endif %}
module.exports = function (context) {
//extract context as local variables
for(let varname of Object.keys(context)){
this[varname]= context[varname]
}
let __njsOutput = ''
__njsOutput += '<p>The page content ... </p>'