This file contains 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
new stream.Duplex({ | |
read(size) {}, | |
write(chunk, encoding, callback) { | |
this.push(chunk) | |
callback() | |
} | |
}) |
This file contains 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
const {adapter, redirect, response} = require("spirit").node | |
const route = require("spirit-router") | |
const redirector = () => { | |
if (true) { | |
return redirect("/another_page") | |
} | |
// both returns are equal below, can use some helper functions for creating | |
// a response, just like redirect() will create a redirect response | |
return response("Condition failed, didn't redirect") |
This file contains 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
const get_user = (name) => { | |
return users.findOne({ name: name }) | |
} | |
const get_likes = (name) => { | |
// get user info | |
return get_user(name).then((user) => { | |
return likes.findAll({ id: user.id }) | |
}) | |
} |
This file contains 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
const home = () => { | |
return <Some React Component> | |
} | |
route.define([ | |
route.get("/home", home) | |
]) | |
// above code when ran on node.js (server) will server render the react component and send back to browser | |
// above code when ran on client will mount the react component home returns |
This file contains 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
const {response} = require("spirit").node | |
(handler) => { | |
return async (request) => { | |
const userExists = await calltoDB(request.user) | |
if (!userExists) return response("you have to login!") | |
return handler(request) | |
} |
This file contains 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
const final_handler = (request) => { | |
return "hi!" | |
} | |
const a = (handler) => { // handler is `b` | |
return async (request) => { | |
return handler(request) | |
} | |
} |
This file contains 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
const buf = Buffer.alloc(65537) // 64 * 1024 = 65536 (+ 1 to go over) | |
const net = require("net") | |
net.createServer((client) => { | |
client.on("data", (data) => { | |
console.log("server receiving data") | |
}) | |
}).listen(5000) |
This file contains 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
const spirit = require("spirit").node | |
const catchall = (handler) => (request) => handler(request).then((resp) => { | |
// something beforehand gave a bad response | |
// set our own custom message | |
if (!spirit.is_response(resp)) { | |
return { status: 500, headers: {}, body: "generic error message because of bad response" } | |
} | |
return resp | |
// something beforehand threw an error |