0Pricing
WebSockets & Realtime Systems Programming · บทเรียน

การตั้งค่าโครงการ Node.js

เริ่มต้นโครงการ Node.js ใหม่และติดตั้งไลบรารี WebSocket ที่จำเป็น เช่น 'ws'

การตั้งค่าโครงการ Node.js เป็นบทเรียน WebSockets & Realtime Systems Programming ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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 -v

Initialize 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 -y

Understanding 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 ws

Exploring 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.js

Dependencies 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.json and node_modules.
  • Installed the ws library.
  • 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebSockets & Realtime Systems Programming ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebSockets & Realtime Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การตั้งค่าโครงการ Node.js”

เริ่มต้นโครงการ Node.js ใหม่และติดตั้งไลบรารี WebSocket ที่จำเป็น เช่น 'ws' คุณปฏิบัติ WebSockets & Realtime Systems Programming ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 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 ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การตั้งค่าโครงการ Node.js
  2. การใช้งานตรรกะฝั่งเซิร์ฟเวอร์
  3. การกระจายข้อความไปยังไคลเอนต์
  4. การจัดการห้องและช่องทาง
← กลับไปที่ WebSockets & Realtime Systems Programming