要开始使用 Socket.IO 进行开发,您需要安装 Node 和 npm(节点程序包管理器)。如果您没有这些,请转到节点设置,以在本地系统上安装节点。通过在终端中运行以下命令来确认已安装节点和npm。
node --version npm --version
您应该得到类似于以下内容的输出:
v17.3.0
8.3.0打开终端,并在终端中输入以下内容以创建新文件夹,然后输入以下命令-
$mkdir test-project $cd test-proect $npm init
它会问你一些问题;用以下方式回答他们-
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help json` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (testsocket) test-project
version: (1.0.0)
description: hello learnfk
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to /Users/hubs/Downloads/TestSocket/package.json:
{
"name": "test-project",
"version": "1.0.0",
"description": "hello learnfk",
"main": "index.js",
"scripts": {
"test": "echo\"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Is this OK? (yes) yes
这将创建一个" package.json" 配置文件,现在无涯教程需要安装 Express 和 Socket.IO ,要安装这些文件并将其保存到 package.json 文件,请在终端中的项目目录中输入以下命令。
npm install --save express socket.io
hubs:TestSocket hubs$ npm install --save express socket.io
npm notice created a lockfile as package-lock.json. You should commit this file.
最后一件事是无涯教程应该继续重新启动服务器,进行更改时,无涯教程将需要一个名为nodemon的工具,要安装 nodemon ,请打开您的终端并输入以下命令-
npm install -g nodemon
hubs:TestSocket hubs$ npm install -g nodemon
/usr/local/bin/nodemon -> /usr/local/lib/node_modules/nodemon/bin/nodemon.js
> [email protected] postinstall /usr/local/lib/node_modules/nodemon
> node bin/postinstall || exit 0
Love nodemon? You can now support the project via the open collective:
> https://opencollective.com/nodemon/donate
added 121 packages from 58 contributors in 6.899s
每当您需要启动服务器时,只需使用 nodemon app.js 而不是使用 node app.js 即可,这将确保您无需在更改文件时重新启动服务器。加快了开发过程。
现在,无涯教程已经创建了开发环境。现在让无涯教程开始使用Socket.IO开发实时应用程序。
参考链接
https://www.learnfk.com/socket.io/socket.io-environment.html
标签:npm,Socket,package,nodemon,无涯,json,IO,test From: https://blog.51cto.com/u_14033984/9444481