إعداد مشروع Node.js
هيّئوا مشروع Node.js جديدًا وثبّتوا مكتبة WebSocket اللازمة، مثل 'ws'.
إعداد مشروع Node.js درس مجاني في WebSockets & Realtime Systems Programming على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة WebSockets & Realtime Systems Programming، انتقل إلى CoddyKit PRO. تتضمن دورة WebSockets & Realtime Systems Programming 4 دروس في المجموع.
ماذا ستتعلم في «إعداد مشروع Node.js»؟
هيّئوا مشروع Node.js جديدًا وثبّتوا مكتبة WebSocket اللازمة، مثل 'ws'. تتمرن على WebSockets & Realtime Systems Programming مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ WebSockets & Realtime Systems Programming؟
لا تُشترط خبرة سابقة. WebSockets & Realtime Systems Programming على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «إعداد مشروع Node.js»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس WebSockets & Realtime Systems Programming هذا؟
نعم. كل درس في WebSockets & Realtime Systems Programming يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إعداد مشروع Node.js
- تنفيذ المنطق من جهة الخادم
- بث الرسائل إلى العملاء
- إدارة الغرف والقنوات