设置 Node.js 项目
初始化一个新的 Node.js 项目,并安装必要的 WebSocket 库(例如“ws”)。
设置 Node.js 项目 是 CoddyKit 上的免费 WebSockets & Realtime Systems Programming 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 WebSockets & Realtime Systems Programming 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 WebSockets & Realtime Systems Programming 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Welcome: Setting Up Node.js
Get ready to build your first WebSocket server! Our journey begins by setting up a new Node.js project. Node.js is a powerful JavaScript runtime that lets you run JavaScript code outside of a web browser.
This lesson will guide you through initializing your project and installing essential libraries.
Verify Node.js Installation
First things first, let's ensure Node.js and its package manager, npm (Node Package Manager), are installed on your system. Open your terminal or command prompt and run these commands:
node -v
npm -vInitialize Your Project Folder
Every Node.js project typically lives in its own folder. Let's create one and then use npm init to set up the project. The -y flag answers all prompts with default values.
mkdir my-websocket-server
cd my-websocket-server
npm init -yUnderstanding package.json
After running npm init -y, you'll find a new file named package.json in your project folder. This file is crucial! It acts as a manifest for your project, containing metadata and a list of all external libraries (dependencies) your project relies on.
{
"name": "my-websocket-server",
"version": "1.0.0",
"description": "My first Node.js WebSocket server",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}Installing the 'ws' Library
To build a WebSocket server, we need a special library to handle the WebSocket communication protocol. The ws library is a very popular, fast, and simple choice for Node.js.
Let's install it using npm:
npm install wsExploring node_modules
Once ws is installed, you'll see a new folder named node_modules appear in your project directory. This is where npm stores all the packages your project depends on, including ws and any of its own dependencies.
It can get quite large! You generally don't modify files inside node_modules directly.
Your First Node.js Script
Let's create a simple JavaScript file, index.js, to confirm our Node.js setup is working. In your project folder, create index.js and add this code:
// index.js
console.log("Hello CoddyKit from Node.js!");Running Your Node.js Script
Now that you have your index.js file, you can run it directly using the node command in your terminal. Make sure you are in your project's root directory.
node index.jsDependencies in package.json
After installing ws, if you open your package.json again, you'll notice a new "dependencies" section. This lists all the external packages your project needs to function, ensuring others can easily set up your project.
{
"name": "my-websocket-server",
"version": "1.0.0",
"description": "My first Node.js WebSocket server",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"ws": "^8.18.0"
}
}Quick Check: Project Setup
You've learned the basics of setting up a Node.js project. Let's test your knowledge!
Recap & Next Steps
Fantastic work! You've successfully set up your first Node.js project for building a WebSocket server. Here's what we covered:
- Verified Node.js and npm installations.
- Initialized a new project with
npm init. - Understood the role of
package.jsonandnode_modules. - Installed the
wslibrary. - Ran a basic Node.js script.
Next, we'll dive into writing the actual server-side logic to handle WebSocket connections!
常见问题解答
「设置 Node.js 项目」课时是免费的吗?
是的 — 「设置 Node.js 项目」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 WebSockets & Realtime Systems Programming 课程的其余内容,请升级到 CoddyKit PRO。 WebSockets & Realtime Systems Programming 课程共包含 4 节课。
「设置 Node.js 项目」这节课中我会学到什么?
初始化一个新的 Node.js 项目,并安装必要的 WebSocket 库(例如“ws”)。 你通过在浏览器中直接运行的动手代码来练习 WebSockets & Realtime Systems Programming,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 WebSockets & Realtime Systems Programming 需要有经验吗?
无需任何先前经验。CoddyKit 上的 WebSockets & Realtime Systems Programming 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「设置 Node.js 项目」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 WebSockets & Realtime Systems Programming 课中编写并运行代码吗?
能。每节 WebSockets & Realtime Systems Programming 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。