K8S学习笔记-------2.极简易懂的入门示例
1. 准备应用代码
编写Node.js应用代码,并确保它可以在本地运行。
1.1 确保 Node.js 和 npm 已安装
在使用 npm 之前,需要先安装 Node.js。因为 npm 是随 Node.js 一起安装的。你可以通过以下命令检查它们是否已经安装以及查看版本信息:
zgq@docker01:~$ node -v
Command 'node' not found, but can be installed with:
sudo apt install nodejs
zgq@docker01:~$ npm -v
Command 'npm' not found, but can be installed with:
sudo apt install npm
我的机器没有安装npm、node ,以下执行安装操作,(先安装node)
先执行对系统的更新操作
root@docker01:/home/zgq# apt update
root@docker01:/home/zgq# apt upgrade
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following upgrades have been deferred due to phasing:
libunwind8 python3-distupgrade ubuntu-release-upgrader-core
0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.
zgq@docker01:~$ sudo apt install nodejs
sudo: unable to resolve host docker01: Name or service not known
[sudo] password for zgq:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
libcares2 libnode109 node-acorn node-busboy node-cjs-module-lexer node-undici node-xtend nodejs-doc
Suggested packages:
npm
The following NEW packages will be installed:
libcares2 libnode109 node-acorn node-busboy node-cjs-module-lexer node-undici node-xtend nodejs nodejs-doc
0 upgraded, 9 newly installed, 0 to remove and 143 not upgraded.
Need to get 16.1 MB of archives.
After this operation, 70.4 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
再安装npm
root@docker01:/home/zgq# apt install npm
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
npm is already the newest version (9.2.0~ds1-2).
0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.
验证版本信息
zgq@docker01:~$ sudo node -v
v18.19.1
zgq@docker01:~$ sudo npm -v
9.2.0
如果显示出版本号,说明已经安装成功。
1.2. 创建项目目录并初始化项目
如果你还没有项目目录,可以创建一个新的目录,并在该目录下初始化一个新的 Node.js 项目。以下是具体操作:
# 创建一个新的项目目录
zgq@docker01:~$ mkdir my-express-app
# 进入项目目录
zgq@docker01:~$ cd my-express-app
# 初始化项目,会生成一个 package.json 文件
zgq@docker01:~/my-express-app$ npm init -y
Wrote to /home/zgq/my-express-app/package.json:
{
"name": "my-express-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
npm init -y 中的 -y 选项表示使用默认配置快速初始化项目,这样会自动生成一个 package.json 文件,该文件用于记录项目的元数据和依赖信息。
1.3. 安装 Express
- 在项目目录下执行 npm install express 命令:
zgq@docker01:~/my-express-app$ npm install express
added 69 packages, and audited 70 packages in 6s
14 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
- 执行该命令后,npm 会从 npm 仓库下载 Express 及其所有依赖项,并将它们安装到项目目录下的 node_modules 文件夹中。同时,package.json 文件会更新,添加 Express 作为项目的依赖项,如下所示:
zgq@docker01:~/my-express-app$ cat package.json
{
"name": "my-express-app",
"version"


被折叠的 条评论
为什么被折叠?



