Randomblue
Randomblue

Reputation: 116383

Command line for running server

I have a Node server which I would like to debug. Once I've started the server

node server.js

I want to execute functions defined in server.js from a command line. The usual Node REPL "blocks" after the server has started.

For example, if server.js defines the function addBlogPost I want to locally call addBlogPost() and observe changes in the database without passing through a GUI.

Is there an easy way to do this?

Upvotes: 2

Views: 4414

Answers (1)

sarnold
sarnold

Reputation: 104080

You can use the repl module to create a new REPL instance:

repl = require("repl")
r = repl.start("node> ")
r.context.pause = pauseHTTP;
r.context.resume = resumeHTTP;

Now inside the REPL you can use pause() to call pauseHTTP() and resume() to call resumeHTTP().

Upvotes: 3

Related Questions