1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > node.js启动应用命令_如何使用Node.js构建命令行应用程序

node.js启动应用命令_如何使用Node.js构建命令行应用程序

时间:2021-11-12 06:23:20

相关推荐

node.js启动应用命令_如何使用Node.js构建命令行应用程序

node.js启动应用命令

As a developer, chances are you spend most of your time in your terminal, typing in commands to help you get around some tasks.

作为开发人员,您很可能会花费大部分时间在终端上,输入命​​令来帮助您解决一些任务。

Some of these commands come built into your Operating System, while some of them you install through some third party helper such as npm, or brew, or even downloading a binary and adding it to your$PATH.

其中一些命令内置在您的操作系统中,而另一些命令则通过第三方帮助程序(例如npm或brew)安装,甚至下载二进制文件并将其添加到$PATH

A good example of commonly used applications include npm, eslint, typescript, and project generators, such as Angular CLI, Vue CLI or Create React App.

常用应用程序的一个很好的例子包括npm,eslint,打字稿和项目生成器,例如Angular CLI , Vue CLI或Create React App 。

In this tutorial you’ll build two small CLI applications in Node.js:

在本教程中,您将在Node.js中构建两个小型CLI应用程序:

Quote Of The Day tool that retrieves quotes of the day from https://quotes.rest/qod.

每日报价工具,可从https://quotes.rest/qod检索当天的报价。

A To-Do List app that uses JSON to save data.使用JSON保存数据的待办事项列表应用程序。

先决条件 (Prerequisites)

To complete this tutorial, you will need:

要完成本教程,您将需要:

A local development environment for Node.js. Follow How to Install Node.js and Create a Local Development Environment

Node.js的本地开发环境。 遵循如何安装Node.js和创建本地开发环境

步骤1 –了解Shebang (Step 1 – Understanding the Shebang)

Whenever you look at any scripting file, you’ll see characters like these in the beginning of the file:

每当您查看任何脚本文件时,都将在文件开头看到以下字符:

file.sh#!/usr/bin/env sh

Or this:

或这个:

file.py#!/usr/bin/env python -c

They serve as a way for your operating system program loader to locate and use toe parse the correct interpreter for your executable file. This only works in Unix Systems though.

它们是您的操作系统程序加载程序找到并使用它为可执行文件解析正确的解释器的一种方式。 不过,这仅适用于Unix系统。

From Wikipedia:

从维基百科 :

In computing, a shebang is the character sequence consisting of the characters number sign and exclamation mark (#!) at the beginning of a script.

在计算中,shebang是由脚本开头的字符数字符号和感叹号(#!)组成的字符序列。

NodeJS has its own supported shebang characters.

NodeJS具有自己支持的shebang字符。

Create a new file in your editor calledlogger.js:

在名为logger.js的编辑器中创建一个新文件:

nano logger.js 纳米logger.js

Add the following code:

添加以下代码:

#!/usr/bin/env nodeconsole.log("I am a logger")

The first line tells the program loader to parse this file with NodeJS. The second line prints text to the screen.

第一行告诉程序加载器使用NodeJS解析此文件。 第二行将文本打印到屏幕上。

You can try and run the file by typing this in your terminal. You’ll get a permission denied for execution.

您可以通过在终端中键入此文件来尝试运行该文件。 您将获得拒绝执行的权限。

./logger ./logger

Outputzsh: permission denied: ./logger

You need to give the file execution permissions. You can do that with.

您需要授予文件执行权限。 您可以这样做。

chmod +x logger chmod + x记录器 ./logger ./logger

This time you’ll see the output.

这次您将看到输出。

OutputI am a logger

You could have run this program withnode logger, but adding the shebang and making the program executable own command lets you avoid typingnodeto run it.

您可以使用node logger运行该程序,但是添加shebang并使该程序可执行自己的命令可以避免键入node来运行它。

创建每日报价应用程序 (Creating the Quote of the Day App)

Let’s create a directory and call itqod. And inside, instantiate a NodeJs app.

让我们创建一个目录并将其命名为qod。 在内部,实例化NodeJs应用程序。

mkdir qod mkdir qod cd qod cd qod npm init -y npm初始化-y

Next, we know we need to make requests to the quotes server, so we could use existing libraries to do just this. We’ll use axios

接下来,我们知道我们需要向报价服务器发出请求,因此我们可以使用现有的库来做到这一点。 我们将使用axios

npm install --save axios

We’ll also add a chalk, a library to help us print color in the terminal.

我们还将添加一个粉笔 ,一个库来帮助我们

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。