构建node.js基础镜像_在Android上构建Node.js应用程序

news/2024/7/8 0:49:26

构建node.js基础镜像

by Aurélien Giraud

通过AurélienGiraud

在Android上构建Node.js应用程序-第1部分:Termux,Vim和Node.js (Building a Node.js application on Android - Part 1: Termux, Vim and Node.js)

If you are excited about Node.js and own an Android device, no doubt you’ll enjoy running Node.js on it. Thanks to Termux a terminal emulator and Linux environment for Android, the fun of developping Node.js web applications is just a few ‘npm install’s away!

如果您对Node.js感到兴奋并拥有Android设备,那么毫无疑问,您将喜欢在其上运行Node.js。 得益于Termux,它是适用于Android的终端仿真器和Linux环境,开发Node.js Web应用程序的乐趣就已经不多了!

我们要做什么 (What we are going to do)

I will show how to get started with Node.js using Termux on Android. We are also going to use Express and see how to store our app’s data in NeDB, a lightweight JavaScript database, whose API is a subset of MongoDB’s.

我将展示如何在Android上使用Termux来开始使用Node.js。 我们还将使用Express,看看如何将我们的应用程序数据存储在NeDB(轻量级JavaScript数据库)中,该数据库的API是MongoDB的子集。

In this first post, we will limit ourselves to setting up our Node.js development environment, that is:

在第一篇文章中,我们将仅限于设置Node.js开发环境,即:

  1. Install and configure Termux.

    安装和配置Termux。

  2. Install and see how to use Vim as a text editor. (This section can be skipped if you already know Vim.)

    安装并查看如何将Vim用作文本编辑器。 (如果您已经了解Vim,则可以跳过本节。)

  3. Install and run Node.js.

    安装并运行Node.js。

1. Termux (1. Termux)

Termux combines terminal emulation with a Linux package collection. It comes as a free app that can be installed directly from the Play Store or from F-Droid catalogue.

Termux将终端仿真与Linux软件包集合结合在一起。 它是免费的应用程序,可以直接从Play商店或F-Droid目录中安装。

组态 (Configuration)

When you open Termux, you are greeted by a Command Line Interface (CLI). Right after installing Termux, it is recommended to check for updates, and upgrade if need be. So type the following commands at the prompt — that is, after the ‘$’ sign — and press <Enter>:

当您打开Termux时, 命令行界面 (CLI)会打招呼。 建议在安装Termux之后立即检查更新,并在需要时进行升级。 因此,在提示符下(即在“ $”符号后)键入以下命令,然后按<Enter>键:

$ apt update && apt upgrade

Termux comes with a minimal base system, so you should also install ‘coreutils’ for the full-fledged variants of base CLI utilities such as ‘mv’, ‘ls’, etc.

Termux带有最小的基本系统,因此您还应该为基本CLI实用程序的完整变体( 例如“ mv”,“ ls”等)安装“ coreutils” 。

$ apt install coreutils

存储 (Storage)

There are three main types of storage in Termux:

Termux有三种主要的存储类型:

  1. App-private storage: This is right where you are when you start Termux.

    应用程序专用存储 :这是启动Termux时的正确位置。

  2. Shared internal storage: Storage in the device available to all apps.

    共享内部存储空间 :设备中可供所有应用程序使用的存储空间

  3. External storage: Storage on external SD cards.

    外部存储 :存储在外部SD卡上。

Although the environment setup in Termux is similar to that of a modern Linux distribution, running on Android implies differences and so far I have only managed to run Node.js fully while storing my data in Termux’s private storage (option 1 above).

尽管Termux中的环境设置与现代Linux发行版中的环境设置相似,但在Android上运行意味着有所不同,到目前为止,我仅设法完全运行Node.js,同时将数据存储在Termux的私有存储中(上述选项1)。

So let’s create a directory for our app and change to this directory:

因此,让我们为我们的应用程序创建一个目录并更改为该目录:

$ mkdir test-node && cd test-node

键盘 (Keyboard)

I have only been using a soft keyboard so far and I encountered some issues with the default touch keyboard while using the volume up key as a replacement for <Esc>, <Tab> or the Arrow keys.

到目前为止,我仅使用软键盘,并且在使用音量增大键代替<Esc>,<Tab>或方向键时遇到了默认触摸键盘的一些问题。

To circumvent these issues, I installed Hacker’s Keyboard from the Play Store and I really like it. It is a touch keyboard that can be used instead of the default one and has all the keys needed for writing code and using the terminal.

为了解决这些问题,我从Play商店安装了Hacker's Keyboard ,非常喜欢。 它是一种触摸键盘,可以代替默认键盘使用,并且具有编写代码和使用终端所需的所有键。

You can find useful information about using a touch or hardware keyboard with Termux directly on the Help page.

您可以直接在“帮助”页面上找到有关将触摸式或硬件键盘与Termux一起使用的有用信息。

使用多个会话 (Using multiple sessions)

One more thing I would like to mention about Termux: if you swipe the screen left to right from its left edge, it opens a menu that enables to start or switch between multiple Termux sessions.

关于Termux,我还要提到的一件事:如果您从屏幕的左边缘向左向右滑动屏幕,它将打开一个菜单,该菜单可以启动或在多个Termux会话之间进行切换。

在Termux中访问帮助 (Accessing Help in Termux)

In Termux, you can access the help documentation, which contains all the necessary information, by long pressing the screen, and clicking first on ‘More’, then on ‘Help’. Note though, that this help documentation cannot be accessed when your device isn’t connected to the internet.

在Termux中,您可以通过长按屏幕并首先单击“更多”,然后单击“帮助”来访问包含所有必要信息的帮助文档。 但是请注意,当您的设备未连接到Internet时,无法访问此帮助文档。

2. Vim (2. Vim)

Vim is a text editor that can be used right in the Command Line Interface and it is available as a package in Termux. So let’s install it:

Vim是一个文本编辑器,可以直接在命令行界面中使用,并且可以在Termux中作为软件包使用。 因此,让我们安装它:

$ apt install vim

Vim’s interface is not based on menus or icons but on commands given in a text user interface. In case you are new to it I’m going to guide you through the very basics of Vim.

Vim的界面不是基于菜单或图标,而是基于文本用户界面中给出的命令。 如果您是新手,我将指导您了解Vim的基础知识。

First, create the file ‘server.js’:

首先,创建文件“ server.js”:

$ touch server.js

To edit this file with Vim, simply type:

要使用Vim编辑该文件,只需键入:

$ vim server.js

使用不同的模式 (Using the different modes)

Vim behaves differently, depending on which mode you are in. At start, you are in what is called command mode. You should see a cursor on the first line, tildes (~) on the other lines and the name of the file at the very bottom.

Vim的行为会有所不同,具体取决于您所处的模式。一开始,您就处于所谓的command mode 。 您应该在第一行上看到一个光标,在其他行上看到波浪号(〜),在最底部看到文件名。

Tilde lines are here to indicate that these lines are not part of the content of the file.

波浪线表示这些行不是文件内容的一部分。

To start writing into the file, you need to switch to writing mode. So just type the letter “i”. At the very bottom, you should now see something like this:

要开始写入文件,您需要切换到写入模式。 因此,只需键入字母“ i”。 在最底部,您现在应该看到类似以下内容:

So now go on. Write something.

所以现在继续。 写一些东西。

Done? So here is how you can save your changes/quit Vim. First you need to come back to the command mode by pressing <Esc> and then you have the choice:

做完了吗 因此,这是保存更改/退出Vim的方法。 首先,您需要按<Esc>键回到命令模式 ,然后您可以选择:

  1. Type :w and press <Enter>to save (write) the changes.

    键入:w并按<Enter>键保存(写入)更改。

  2. Type :wq and press <Enter> to save the changes and quit.

    键入:wq并按<Enter>键保存更改并退出。

  3. Type :q! and press <Enter> to quit without saving the changes.

    输入:q! 然后按<Enter>键退出而不保存更改。

And that is about it for our very short introduction to Vim.

这就是我们对Vim的简短介绍。

不会迷路并了解有关Vim的更多信息 (Not getting lost and learning more about Vim)

If you are lost, you can press <Esc> and type :help followed by <Enter>. This will open Vim help documentation.

如果您迷路了,可以按<Esc>并键入 :help,然后按<Enter>。 这将打开Vim帮助文档。

Something like this simple Vim Reference might be useful if you are new to Vim. Alternatively, you can type ‘vimtutor’ in the terminal for a 30 minutes tutorial, play a learning game at http://vim-adventures.com/ or follow the interactive tutorial at http://www.openvim.com/.

如果您是Vim的新手,那么像这个简单的Vim Reference这样的内容可能会很有用。 或者,您可以在终端中输入“ vimtutor”进行30分钟的教程,在http://vim-adventures.com/上玩学习游戏,或在http://www.openvim.com/上遵循交互式教程。

3. Node.js (3. Node.js)

Installing Node.js is very simple:

安装Node.js非常简单:

$ apt install nodejs

If you haven’t done it yet, create a folder for the application, move into it and type:

如果尚未完成,请为该应用程序创建一个文件夹,进入其中并键入:

$ npm init

This will ask you a bunch of questions, and then write a ‘package.json’ file for you. (You can just press <Enter> for each question asked.)

这会问您很多问题,然后为您编写一个“ package.json”文件。 (您可以对每个问题按<Enter>键。)

Now let us check that everything is working all right. Open server.js

现在让我们检查一切是否正常。 打开server.js

$ vim server.js

and type in it

并输入

console.log('This is Node.js running on Android.')

Save the changes and quit Vim.

保存更改并退出Vim。

Now we have everything in place and we can finally run node:

现在我们已经准备就绪,终于可以运行node了:

$ node server.js

This should print the text “This is Node.js running on Android.” in the terminal.

这应该打印文本“这是在Android上运行的Node.js。” 在终端。

简而言之 (In a nutshell)

As a recap, here is the whole process again (with minor differences as it is all done directly from the command line).

作为回顾,这是整个过程(略有不同,因为它们都是直接从命令行完成的)。

Update and upgrade Termux:  $ apt update && apt upgrade
Install some core utilities, Vim and Node.js:  $ apt install coreutils  $ apt install vim  $ apt install nodejs
Create a directory called test-node and move into it:  $ mkdir test-node && cd test-node
Create an empty file called server.js:  $ touch server.js
Interactively create a package.json file:  $ npm init
Add some content to server.js:  $ echo “console.log(‘This is Node.js running on Android.’)” > server.js
Run node:  $ node server.js

结语 (Wrapping it up)

We have seen how to use Termux on Android, how to edit files with Vim and how to run Node.js.

我们已经了解了如何在Android上使用Termux,如何使用Vim编辑文件以及如何运行Node.js。

Here are a the main links related to Termux: its web page, its wiki and its GitHub repositories. It can be installed from the Play Store or from the F-Droid catalogue.

这是与Termux相关的主要链接:其网页 ,其Wiki和GitHub存储库 。 可以从Play商店或F-Droid目录中安装。

In the next post we are going to build a basic Node.js application using the Express web framework and a lightweight JavaScript database called NeDB which uses MongoDB’s API and can be used to develop and run a web application in Termux.

在下一篇文章中,我们将使用Express Web框架和轻量级JavaScript数据库NeDB (使用MongoDB的API,可用于在Termux中开发和运行Web应用程序)构建基本的Node.js应用程序。

In the meantime, happy coding!

同时,祝您编程愉快!

翻译自: https://www.freecodecamp.org/news/building-a-node-js-application-on-android-part-1-termux-vim-and-node-js-dfa90c28958f/

构建node.js基础镜像


http://lihuaxi.xjx100.cn/news/240530.html

相关文章

python标准库介绍——23 UserString 模块详解

UserString 模块(2.0 新增) UserString 模块包含两个类, //UserString// 和 //MutableString// . 前者是对标准字符串类型的封装, 后者是一个变种, 允许你修改特定位置的字符(联想下列表就知道了).注意 //MutableString// 并不是效率很好, 许多操作是通过切片和字符串连接实现…

cloud turbine

turbine是聚合服务器发送事件流数据的一个工具&#xff0c;hystrix的监控中&#xff0c;只能监控单个节点&#xff0c;实际生产中都为集群&#xff0c;因此可以通过 turbine来监控集群下hystrix的metrics情况&#xff0c;通过eureka来发现hystrix服务。 1.断路器1 入口类 Sprin…

ux体验网站 英国_?? 用户体验(UX)资源和工具的完整列表??

ux体验网站 英国by Jason Hreha杰森赫雷哈(Jason Hreha) ?? 用户体验(UX)资源和工具的完整列表?? (?? The Complete List of User Experience (UX) Resources & Tools ??) 超过100个链接&#xff0c;可以链接到最好的书籍&#xff0c;课程&#xff0c;新闻通讯和工…

大众点评数据平台架构变迁

2019独角兽企业重金招聘Python工程师标准>>> 最近和其他公司的同学对数据平台的发展题做了一些沟通&#xff0c;发现各自遇到的问题都类似&#xff0c;架构的变迁也有一定的相似性。 以下从数据&架构&应用的角度对2012.07-2014.12期间大众点评数据平台的架构…

设计模式(2)工厂方法模式(Factory Method)

设计模式&#xff08;0&#xff09;简单工厂模式 设计模式&#xff08;1&#xff09;单例模式&#xff08;Singleton&#xff09; 源码地址 0 工厂方法模式简介 0.0 工厂方法模式定义 工厂方法模式是在简单工厂模式基础上&#xff0c;为解决更复杂的对象创建问题而衍生进化出来…

百练,4103,踩方格

百练&#xff0c;4103&#xff0c;踩方格 普通做法&#xff1a;&#xff08;也可以找规律&#xff09; #include #include//要调用memset函数&#xff0c;头文件 using namespace std; int visited[50][50];//开辟50*50的方格 int num0;//num为方案数 void ways(int i,int j,i…

模糊推理 控制 易于实现_代码“易于推理”是什么意思?

模糊推理 控制 易于实现by Preethi Kasireddy通过Preethi Kasireddy 代码“易于推理”是什么意思&#xff1f; (What does it mean when code is “easy to reason about”?) You’ve probably heard the expression “easy to reason about” enough times to make your ear…

sendmail服务器配置过程中出现的一些错误和解决办法

sendmail服务器配置过程中出现的错误和解决办法 以前在做实验的过程中总结和写的一些教程的一些资料&#xff0c;一直没时间发布到博客上面&#xff0c;五一到了&#xff0c;终于有点时间发布啦&#xff01;关于Linux上面还会有RHCE系列的学习笔记发表 1、rpm包不能正常卸载&am…