简体   繁体   中英

Prereload script into node interactive mode

Is it possibille to run node.exe, pipe a text into it, and continue the interactive session?

I want to create a shortcut bat (or bash) file for editing my database.

Usually this is what I'm doing:

$ node

>var db=require('mydb')
>db.open('myserver')
>//Now I can start access the db
>db.query...

I want to do something like that:

$ node -i perDefinedDb.js

>db.query(.... //I don't want to define the DB each time I run the node.exe

I tried some like that:

echo console.log(a) | node.exe

This is the result:

3

And the program is Finish. I want to continue the node REPL after piping something into.


In Other Words: I want to be able to use my DB from node REPL, without defining it each time.

Launch the REPL from your js file and you can give the context you want:

const repl = require('repl');
var db = require('mydb');
db.open('myserver');

repl.start('> ').context.db = db;

Now you just have to run this file ( node myREPL.js ) and you can REPL as usual.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM