I am trying out Node.js Express framework, and looking for plugin that allows me to interact with my models via console, similar to Rails console. Is there such a thing in NodeJS world?
If not, how can I interact with my Node.js models and data, such as manually add/remove objects, test methods on data etc.?
Rails is a Ruby-based framework, which is written in Ruby language. 2. Node JS is the best suited for developing small size projects and for I/O non-blocking, event-based application. Rails is a web application framework, well suited for database-backed web application in MVC pattern and for metaprogramming.
The node:console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: A Console class with methods such as console.
Performance speed If we compare the speed of Ruby on Rails vs Node. js, even experienced Ruby on Rails developers acknowledge that the framework is slow. While its speed improves with every version, it's still a lot lower than Node's. There are many reasons for the performance delay.
While Ruby on Rails is a framework and, no doubt, has its strength, there are many situations where it falls back. Node. js, on the other hand, can offer the non-blocking I/O paradigm that is excellent for creating data-intensive and real-time apps. That's something RoR can't provide.
Create your own REPL by making a js file (ie: console.js) with the following lines/components:
var repl = require("repl");
var replServer = repl.start({});
replServer.context.<your_variable_names_here> = <your_variable_names_here>
. This makes the variable available/usable in the REPL (node console).For example: If you have the following line in your node app: var db = require('./models/db')
Add the following lines to your console.js
var db = require('./models/db'); replServer.context.db = db;
node console.js
Your console.js file should look something like this:
var repl = require("repl"); var epa = require("epa"); var db = require("db"); // connect to database db.connect(epa.mongo, function(err){ if (err){ throw err; } // open the repl session var replServer = repl.start({}); // attach modules to the repl context replServer.context.epa = epa; replServer.context.db = db; });
You can even customize your prompt like this:
var replServer = repl.start({ prompt: "Node Console > ", });
For the full setup and more details, check out: http://derickbailey.com/2014/07/02/build-your-own-app-specific-repl-for-your-nodejs-app/
For the full list of options you can pass the repl like prompt, color, etc: https://nodejs.org/api/repl.html#repl_repl_start_options
Thank you to Derick Bailey for this info.
GavinBelson has a great recommendation for running with sequelize ORM (or anything that requires promise handling in the repl).
I am now running sequelize as well, and for my node console I'm adding the --experimental-repl-await
flag.
It's a lot to type in every time, so I highly suggest adding:
"console": "node --experimental-repl-await ./console.js"
to the scripts
section in your package.json
so you can just run:
npm run console
and not have to type the whole thing out.
Then you can handle promises without getting errors, like this:
const product = await Product.findOne({ where: { id: 1 });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With