Node.js的Rails控制台的等价性

时间:2022-04-17 20:42:54

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?

我正在尝试Node.js Express框架,并寻找允许我通过控制台与我的模型交互的插件,类似于Rails控制台。在NodeJS世界中有这样的事吗?

If not, how can I interact with my Node.js models and data, such as manually add/remove objects, test methods on data etc.?

如果没有,我如何与我的Node.js模型和数据交互,例如手动添加/删除对象,数据测试方法等?

4 个解决方案

#1


5  

It's simple: add a REPL to your program

这很简单:在程序中添加REPL

#2


33  

Create your own REPL by making a js file (ie: console.js) with the following lines/components:

通过使用以下行/组件创建一个js文件(即:console.js)来创建自己的REPL:

  1. Require node's built-in repl: var repl = require("repl");
  2. 需要节点的内置repl:var repl = require(“repl”);
  3. Load in all your key variables like db, any libraries you swear by, etc.
  4. 加载所有关键变量,如db,您发誓的任何库,等等。
  5. Load the repl by using var replServer = repl.start({});
  6. 使用var replServer = repl.start({})加载repl;
  7. Attach the repl to your key variables with replServer.context.<your_variable_names_here> = <your_variable_names_here>. This makes the variable available/usable in the REPL (node console).
  8. 使用replServer.context将repl附加到您的密钥变量。 = 。这使得变量在REPL(节点控制台)中可用/可用。

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')将以下行添加到console.js

 var db = require('./models/db');
 replServer.context.db = db;
  1. Run your console with the command node console.js
  2. 使用命令node console.js运行控制台

Your console.js file should look something like this:

您的console.js文件应如下所示:

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/

有关完整设置和更多详细信息,请访问: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

对于完整的选项列表,您可以传递repl,如提示,颜色等:https://nodejs.org/api/repl.html#repl_repl_start_options

Thank you to Derick Bailey for this info.

感谢Derick Bailey提供此信息。

#3


19  

I am not very experienced in using node, but you can enter node in the command line to get to the node console. I then used to require the models manually

我在使用节点方面不是很有经验,但您可以在命令行中输入节点以进入节点控制台。然后我曾经手动要求模型

#4


4  

This may not fully answer your question, but to clarify, node.js is much lower-level than Rails, and as such doesn't prescribe tools and data models like Rails. It's more of a platform than a framework.

这可能无法完全回答您的问题,但为了澄清,node.js比Rails低得多,因此没有规定像Rails这样的工具和数据模型。它更像是一个平台,而不是一个框架。

If you are looking for a more Rails-like experience, you may want to look at a more 'full-featured' framework built on top of node.js, such as Meteor, etc.

如果您正在寻找更像Rails的体验,您可能希望看一个基于node.js构建的更全面的“全功能”框架,例如Meteor等。

#1


5  

It's simple: add a REPL to your program

这很简单:在程序中添加REPL

#2


33  

Create your own REPL by making a js file (ie: console.js) with the following lines/components:

通过使用以下行/组件创建一个js文件(即:console.js)来创建自己的REPL:

  1. Require node's built-in repl: var repl = require("repl");
  2. 需要节点的内置repl:var repl = require(“repl”);
  3. Load in all your key variables like db, any libraries you swear by, etc.
  4. 加载所有关键变量,如db,您发誓的任何库,等等。
  5. Load the repl by using var replServer = repl.start({});
  6. 使用var replServer = repl.start({})加载repl;
  7. Attach the repl to your key variables with replServer.context.<your_variable_names_here> = <your_variable_names_here>. This makes the variable available/usable in the REPL (node console).
  8. 使用replServer.context将repl附加到您的密钥变量。 = 。这使得变量在REPL(节点控制台)中可用/可用。

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')将以下行添加到console.js

 var db = require('./models/db');
 replServer.context.db = db;
  1. Run your console with the command node console.js
  2. 使用命令node console.js运行控制台

Your console.js file should look something like this:

您的console.js文件应如下所示:

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/

有关完整设置和更多详细信息,请访问: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

对于完整的选项列表,您可以传递repl,如提示,颜色等:https://nodejs.org/api/repl.html#repl_repl_start_options

Thank you to Derick Bailey for this info.

感谢Derick Bailey提供此信息。

#3


19  

I am not very experienced in using node, but you can enter node in the command line to get to the node console. I then used to require the models manually

我在使用节点方面不是很有经验,但您可以在命令行中输入节点以进入节点控制台。然后我曾经手动要求模型

#4


4  

This may not fully answer your question, but to clarify, node.js is much lower-level than Rails, and as such doesn't prescribe tools and data models like Rails. It's more of a platform than a framework.

这可能无法完全回答您的问题,但为了澄清,node.js比Rails低得多,因此没有规定像Rails这样的工具和数据模型。它更像是一个平台,而不是一个框架。

If you are looking for a more Rails-like experience, you may want to look at a more 'full-featured' framework built on top of node.js, such as Meteor, etc.

如果您正在寻找更像Rails的体验,您可能希望看一个基于node.js构建的更全面的“全功能”框架,例如Meteor等。