Skip to content

Instantly share code, notes, and snippets.

/**
* @description Find predicate to get the current route
* @param m Method from request
* @param p Path from request
* @returns {function(*)}
* @private
*/
_findAndComposeRoute(m, p) {
return route => {
@bpinedah
bpinedah / match-path-example.js
Created March 17, 2020 00:25
Router article
/**
* @description Get path parameters by router route
* @param routePath Path from route
* @param requestPath Path from request
* @returns {{params: {}, base: *}}
* @private
*/
_getPathParams(routePath, requestPath) {
const { match } = require("path-to-regexp");
const _match = match(routePath, {decode: decodeURIComponent});
@bpinedah
bpinedah / route-object.js
Created March 16, 2020 09:57
Router article
const route = {
path: "/users",
method: "get",
middlewares: [
[Function],
[Function],
[Function]
}
}
@bpinedah
bpinedah / verbose-methods.js
Created March 16, 2020 09:45
Router article
const _protoRouter = {
_routes: [],
/**
* @description Add new route to list
* @param method HTTP method from request
* @param path Route path to add
* @param middlewares Middlewares functions to add
* @private
*/
_addRoutes(method, path, middlewares) {
@bpinedah
bpinedah / logic-description.js
Created March 16, 2020 09:31
Logic description for a router
router.get('/users', async (req, res) => {
// Get information from DB, files, etc and send it.
return res.send([...]);
});
@bpinedah
bpinedah / pipeP-then.js
Created December 19, 2018 16:13
Pipes article
pip.then(r => console.log(r));
@bpinedah
bpinedah / pipeP-full.js
Last active December 19, 2018 16:20
Pipes article
const p1 = (n) => new Promise(resolve => {
setTimeout(() => resolve(n * 2), 3000);
});
const p2 = (n) => new Promise(resolve => {
setTimeout(() => resolve(n + 1), 2000);
});
const p3 = (n) => new Promise(resolve => {
setTimeout(() => resolve(n + 3), 1000);
});
const pipeP = (...fns) => (
@bpinedah
bpinedah / pipeP.js
Last active December 19, 2018 16:11
Pipes article
const pipeP = (...fns) => (
fns.reduce((f, g) => x => f(x).then(g))
);
@bpinedah
bpinedah / pipes.js
Created December 19, 2018 01:00
Pipes article
const pipe = (...fns) => x => fns.reduce((f, m) => m(f), x);
const fn1 = n => n * 2;
const fn2 = n => n + 2;
const fn3 = n => n - 4;
const pip = pipe(
fn1,
fn2,
fn3
)(10);
@bpinedah
bpinedah / promise-chain.js
Created December 19, 2018 00:41
Pipes article
const p1 = (n) => new Promise(resolve => {
setTimeout(() => resolve(n * 2), 3000);
});
const p2 = (n) => new Promise(resolve => {
setTimeout(() => resolve(n + 1), 2000);
});
const p3 = (n) => new Promise(resolve => {
setTimeout(() => resolve(n + 3), 1000);
});