Skip to content

Instantly share code, notes, and snippets.

@mxriverlynn
Last active November 20, 2019 01:58
Show Gist options
  • Save mxriverlynn/8ef0c791cf34b7774f91 to your computer and use it in GitHub Desktop.
Save mxriverlynn/8ef0c791cf34b7774f91 to your computer and use it in GitHub Desktop.
build your own app-specific REPL for a nodejs application http://derickbailey.com/2014/07/02/build-your-own-app-specific-repl-for-your-nodejs-app/
// my-repl.js
var repl = require("repl");
var replServer = repl.start({
prompt: "my-app > ",
});
replServer.context.foo = "bar";
// my-repl.js
var repl = require("repl");
var replServer = repl.start({
prompt: "my-app > ",
});
$ node my-repl
my-app > var foo = "bar";
undefined
my-app > foo
'bar'
my-app > var fs = require("fs")
undefined
my-app >
$ node my-repl.js
my-app > foo
'bar'
my-app >
$ node
> var foo = "bar";
undefined
> foo
'bar'
> var fs = require("fs")
undefined
>
var repl = require("repl");
// environment configuration
var epa = require("epa");
// database
var db = require("db");
// app specific modules
var Podcasts = require("podcasts");
var Accounts = require("accounts");
// i love moment.js
var moment = require("moment");
// connect to the database
db.connect(epa.mongo, function(err){
if (err){ throw err; }
var envName = process.env.NODE_ENV || "dev";
// open the repl session
var replServer = repl.start({
prompt: "SignalLeaf (" + envName + ") > ",
});
// attach my modules to the repl context
replServer.context.epa = epa;
replServer.context.db = db;
replServer.context.Podcasts = Podcasts;
replServer.context.Accounts = Accounts;
replServer.context.moment = moment;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment