博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
构建node.js基础镜像_在Android上构建Node.js应用程序
阅读量:2524 次
发布时间:2019-05-11

本文共 8822 字,大约阅读时间需要 29 分钟。

构建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 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。 得益于是适用于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 or from catalogue.

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

组态 (Configuration)

When you open Termux, you are greeted by a (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

Termux带有最小的基本系统,因此您还应该为的完整变体( 安装“ coreutils”

$ apt install coreutils

存储 (Storage)

There are three main types of :

有三种主要的类型:

  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 as a replacement for <Esc>, <Tab> or the Arrow keys.

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

To circumvent these issues, I installed 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商店安装了 ,非常喜欢。 它是一种触摸键盘,可以代替默认键盘使用,并且具有编写代码和使用终端所需的所有键。

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

您可以直接在上找到有关将触摸式或硬件键盘与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 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 or follow the interactive tutorial at .

如果您是Vim的新手,那么像这个简单的可能会很有用。 或者,您可以在终端中输入“ vimtutor”进行30分钟的教程,在玩学习游戏,或在遵循交互式教程。

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

Installing is very simple:

安装非常简单:

$ 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 , its and its . It can be installed from the or from the catalogue.

这是与Termux相关的主要链接:其 ,其和 。 可以从或目录中安装。

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

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

In the meantime, happy coding!

同时,祝您编程愉快!

翻译自:

构建node.js基础镜像

转载地址:http://jdyzd.baihongyu.com/

你可能感兴趣的文章
html 自制属性
查看>>
面向对象术语概念
查看>>
细胞(cell) 矩阵快速幂
查看>>
HDU - 1272 小希的迷宫
查看>>
EntityFramework(1)基础概念与Database First
查看>>
Spring Boot 任务
查看>>
2018APIO 进京赶考
查看>>
Duilib程序添加托盘图标显示
查看>>
在windows上搭建redis集群(redis-cluster)
查看>>
【省选十连测之九】【DP】【组合计数去重】【欧拉函数】基本题
查看>>
文件上传功能 -- jquery.form.js/springmvc
查看>>
阿里云ecs(phpstudy一件包)
查看>>
Python核心编程的四大神兽:迭代器、生成器、闭包以及装饰器
查看>>
linux /proc/sys/fs/file-nr /proc/sys/fs/file-max /etc/security/limits.conf 三者的关联
查看>>
AndroidStudio-快捷键
查看>>
用python DIY一个图片转pdf工具并打包成exe
查看>>
6月14 空控制器和空操作及命名空间
查看>>
volicity文法学习和总结
查看>>
block 块的内部结构
查看>>
IDEA修改git账号密码
查看>>