Created
August 16, 2022 17:03
-
-
Save rcoedo/7ad6231e954d92e5691dbc2f39530f3e to your computer and use it in GitHub Desktop.
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
This gist contains the examples for the article "Mastering the Node.js REPL, Part Three". | |
- https://medium.com/trabe/mastering-the-node-js-repl-part-3-c0374be0d1bf | |
- https://rcoedo.com/blog/2018/08/27/mastering-the-node-js-repl-part-three |
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 express = require("express"); | |
const controllers = require("./controllers"); | |
const app = express(); | |
app.get("/users", controllers.getUsers); | |
app.get("/users/:userId", controllers.getUser); | |
app.get("/users/:userId/posts", controllers.getUserPosts); | |
app.get("/posts", controllers.getPosts); | |
app.get("/posts/:postId", controllers.getPost); | |
app.listen(3000, () => console.log("express server listening on port 3000")); |
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
"scripts": { | |
"start": "nodemon main.js", | |
"repl": "./bin/repl-client localhost:3001" | |
} |
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
"scripts": { | |
"start": "nodemon main.js", | |
"repl": "node dev/repl/repl.js" | |
} |
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
#!/usr/bin/env node | |
const net = require("net"); | |
const args = process.argv.slice(2); | |
if (args.length < 1) { | |
console.log("USAGE: repl <HOST:PORT>"); | |
process.exit(1); | |
} | |
const url = args[0]; | |
const [host, port] = url.split(":"); | |
const socket = net.connect(parseInt(port), host); | |
process.stdin.pipe(socket); | |
socket.pipe(process.stdout); | |
socket.on("connect", () => { | |
process.stdin.setRawMode(true); | |
}); | |
socket.on("close", () => { | |
process.exit(0); | |
}); | |
process.on("exit", () => { | |
socket.end(); | |
}); |
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 start = socket => { | |
console.log("repl client connected"); | |
sayWelcome(socket); | |
const repl = Repl.start({ | |
prompt, | |
input: socket, | |
output: socket, | |
terminal: true, | |
}); | |
// define commands... | |
// initialize context... | |
// listen repl events... | |
socket.on("error", e => { | |
console.log(`repl socket error: ${e}`); | |
}); | |
repl.on("exit", () => { | |
console.log("repl client disconnected"); | |
socket.end(); | |
}); | |
}; |
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 net = require("net"); | |
const repl = require("./repl"); | |
const server = net.createServer(socket => { | |
start(socket); | |
}); | |
module.exports = server; |
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 Repl = require("repl"); | |
const { extendWith, colorize, defineCommands, clearRequireCache } = require("./utils"); | |
const { sayWelcome, sayBye, sayDoc, prompt } = require("./cli"); | |
// Define a context initializer | |
const initializeContext = context => { | |
clearRequireCache(); | |
extendWith({ | |
R: require("ramda"), | |
services: require("../../services"), | |
})(context); | |
}; | |
sayWelcome(); | |
const repl = Repl.start({ prompt }); | |
defineCommands({ | |
doc: { | |
help: "Get information about the loaded modules", | |
action() { | |
this.clearBufferedCommand(); | |
sayDoc(); | |
this.displayPrompt(); | |
}, | |
}, | |
})(repl); | |
initializeContext(repl.context); | |
repl.on("reset", initializeContext); | |
repl.on("exit", sayBye); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment