node 中进行命令行交互_使用Node.js构建交互式命令行应用程序

node 中进行命令行交互

JavaScript has evolved over the last five years with the introduction of Node.js. Basically, It serves as a language that can be used for Front end application, server side application, desktop application, mobile application, etc.

在过去的五年中,随着Node.js的推出,JavaScript得到了发展。 基本上,它是一种可用于前端应用程序,服务器端应用程序,桌面应用程序,移动应用程序等的语言。

The ecosystem is growing really fast with different innovation and technologies springing out from it. StackOverflow's recent survey shows that Javascript is by far the most commonly used programming language on earth.

随着各种创新和技术的涌现,生态系统正在Swift发展。 StackOverflow的最新调查表明,Java语言是迄今为止世界上最常用的编程语言。

That said, over the past 5 years, Node.js has allowed developers to write command-line tools that are interactive and very easy to use.

也就是说,在过去的5年中,Node.js允许开发人员编写交互式且易于使用的命令行工具。

If you have used git, heroku, gulp, grunt or any other package that allows you to bootstrap your project like create-react-app, angular-cli, yeoman, etc, you've used the power of Node.js in writing command-line tools. I think it's easy and simple to start writing your own command-line tools today with Node.js and its libraries for increased productivity.

如果您使用过git , heroku , gulp , grunt或其他任何可以引导您的项目的包,例如create-react-app , angular-cli , yeoman等,那么您已经在编写命令时使用了Node.js的强大功能线工具。 我认为现在开始使用Node.js及其库来编写自己的命令行工具既容易又容易,以提高生产率。

项目介绍 ( Project description )

We will be building an interactive command-line contact management system that allows users to create, read, and manage their contacts from the shell(command prompt). This will allow users to perform basic CRUD operation from the shell with ease.

我们将构建一个交互式的命令行联系人管理系统,该系统允许用户从shell(命令提示符)创建,读取和管理其联系人。 这将使用户可以轻松地从Shell执行基本的CRUD操作。

Here is a demo of the application we're going to build

这是我们将要构建的应用程序的演示

We will use Node.js as the core framework, commander.js for command-line interfaces, inquirer.js for gathering user input from the command-line, and MongoDB for data persistence.

我们将使用Node.js的为核心框架, commander.js的命令行界面, inquirer.js从命令行收集用户输入和MongoDB的数据持久性。

技术领域 ( Technologies )

1) Node.js - A server-side platform wrapped around the JavaScript language with the largest package(npm) ecosystem in the world.

1) Node.js-围绕JavaScript语言的服务器端平台,拥有世界上最大的package(npm)生态系统。

2) Commander.js - An elegant and light-weight command-line library for Node.js.

2) Commander.js-一个优雅轻巧的Node.js命令行库。

3) Inquirer.js - A collection of powerful interactive command-line interfaces.

3) Inquirer.js-强大的交互式命令行界面的集合。

4) MongoDB - A free and opensource NoSQL document-oriented database.

4) MongoDB-一个免费的开源NoSQL面向文档的数据库。

Here is a picture of what the application workflow looks like.

node 中进行命令行交互_使用Node.js构建交互式命令行应用程序_第1张图片 Fig: Project workflow

这是应用程序工作流的外观图。

使用Node.js构建交互式命令行应用程序的步骤 ( Steps to building an interactive command-line application with Node.js )

1) Project setup

1)项目设置

2) Define application logic

2)定义应用逻辑

3) Handle command-line arguments

3)处理命令行参数

4) Interactive run time user inputs

4)交互式运行时用户输入

5) Convert application to a shell command

5)将应用程序转换为Shell命令

6) More application logic

6)更多的应用逻辑

第1步(共5步):项目设置 (Step 1 of 5: Project setup)

Ensure the version of your installed Node.js is >=6. Run node --version on your terminal to verify. We will use yarn, a package manager for Javascript developed by Facebook to manage our project dependencies. If you don't have it installed, run

确保已安装的Node.js的版本为> = 6。 在终端上运行node --version以进行验证。 我们将使用yarn ,这是Facebook开发的Java包管理器,用于管理我们的项目依赖项。 如果没有安装,请运行

$ npm install -g yarn

$ npm install -g yarn

You can also use npm to run your application as yarn is just a personal preference though with some advantages.

您还可以使用npm来运行您的应用程序,因为yarn只是个人喜好,尽管有一些优点。

Let's create a project directory and initialize it as a Node.js application.

让我们创建一个项目目录,并将其初始化为Node.js应用程序。

$mkdir contact-manager # Create project directory
$ cd contact-manager && yarn init # Initialize the directory as a Node.jS application and follow the prompt
$ yarn add mongoose inquirer commander # Add the dependencies we need for the project

Now that we have our Node.js application initialized, you will see package.json in your project's root directory with updated dependencies to get started.

现在,我们已经初始化了Node.js应用程序,您将在项目的根目录中看到具有更新的dependencies package.json以开始使用。

{
    
  "name": "contacto",
  "version": "1.0.0",
  "description": "A command-line contact management system",
  ............
 "dependencies": {
    
    "commander": "^2.9.0",
    "inquirer": "^3.0.6",
    "mongoose": "^4.9.2"
  }
}
第2步,共5步:定义应用程序逻辑 (Step 2 of 5: Define application logic)

In this section, we will define our schema, model, and the controller functions that handles user input and persist them to the database.

在本节中,我们将定义用于处理用户输入并将其持久化到数据库的架构,模型和控制器功能。

This step requires that your MongoDB server is running.

此步骤要求您的MongoDB服务器正在运行。

Let's create logic.js in the project directory

让我们在项目目录中创建logic.js

const mongoose = require('mongoose'); // An Object-Document Mapper for Node.js
const assert = require('assert'); // N.B: Assert module comes bundled with Node.js.
mongoose.Promise = global.Promise; // Allows us to use Native promises without throwing error.

// Connect to a single MongoDB instance. The connection string could be that of a remote server
// We assign the connection instance to a constant to be used later in closing the connection
const db = mongoose.connect('mongodb://localhost:27017/contact-manager');

// Converts value to lowercase
function toLower(v) {
     
  return v.toLowerCase();
}

// Define a contact Schema
const contactSchema = mongoose.Schema({
     
  firstname: {
      type: String, set: toLower },

你可能感兴趣的:(数据库,java,python,linux,javascript,ViewUI)