Socket.IO - 环境

Socket.IO - 环境 首页 / Socket.io入门教程 / Socket.IO - 环境

要开始使用 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.

+ express@4.17.1

+ socket.io@2.3.0

最后一件事是无涯教程应该继续重新启动服务器,进行更改时,无涯教程将需要一个名为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

> nodemon@2.0.4 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

+ nodemon@2.0.4

added 121 packages from 58 contributors in 6.899s

每当您需要启动服务器时,只需使用 nodemon app.js 而不是使用 node app.js 即可,这将确保您无需在更改文件时重新启动服务器。加快了开发过程。

现在,无涯教程已经创建了开发环境。现在让无涯教程开始使用Socket.IO开发实时应用程序。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Service Mesh实践指南 -〔周晶〕

白话法律42讲 -〔周甲徳〕

Linux性能优化实战 -〔倪朋飞〕

10x程序员工作法 -〔郑晔〕

全栈工程师修炼指南 -〔熊燚(四火)〕

小马哥讲Spring核心编程思想 -〔小马哥〕

接口测试入门课 -〔陈磊〕

Java业务开发常见错误100例 -〔朱晔〕

WebAssembly入门课 -〔于航〕

好记忆不如烂笔头。留下您的足迹吧 :)