Last active
November 20, 2019 01:58
-
-
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/
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
// my-repl.js | |
var repl = require("repl"); | |
var replServer = repl.start({ | |
prompt: "my-app > ", | |
}); | |
replServer.context.foo = "bar"; |
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
// my-repl.js | |
var repl = require("repl"); | |
var replServer = repl.start({ | |
prompt: "my-app > ", | |
}); |
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
$ node my-repl | |
my-app > var foo = "bar"; | |
undefined | |
my-app > foo | |
'bar' | |
my-app > var fs = require("fs") | |
undefined | |
my-app > |
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
$ node my-repl.js | |
my-app > foo | |
'bar' | |
my-app > |
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
$ node | |
> var foo = "bar"; | |
undefined | |
> foo | |
'bar' | |
> var fs = require("fs") | |
undefined | |
> |
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
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