พื้นฐานกรอบงาน Express.js
เริ่มต้นใช้งาน Express.js ตั้งค่าเซิร์ฟเวอร์พื้นฐาน และจัดการคำขอและการตอบกลับ HTTP
พื้นฐานกรอบงาน Express.js เป็นบทเรียน Node.js Backend Development Bootcamp ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Node.js Backend Development Bootcamp และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Node.js Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Discover Express.js
Express.js is a minimal and flexible Node.js web application framework.
It provides a robust set of features for web and mobile applications.
Think of it as a toolkit that simplifies building powerful server-side applications with Node.js.
Benefits of Express.js
Express.js makes backend development faster and easier. Here's why:
- Routing: Easily define how your app responds to different URLs.
- Middleware: Add functions that process requests before they reach your routes.
- Templating: Integrate with various templating engines for dynamic content.
- Performance: It's lightweight and fast, built on Node.js's non-blocking I/O.
Initialize Your Node.js Project
Before installing Express, you need a Node.js project. We'll use npm init to create a package.json file.
This file tracks your project's metadata and dependencies.
Open your terminal in an empty folder and run:
npm init -yThe -y flag answers "yes" to all prompts, creating a default package.json.
Add Express to Your Project
Now that your project is set up, let's install Express.js. NPM (Node Package Manager) handles this for us.
Run the following command in your project's terminal:
npm install expressThis command downloads Express and its dependencies, adding them to your node_modules folder and listing it in package.json.
Basic Server Setup
Every Express application starts by importing the Express module and creating an app instance.
This app object will be used to configure routes and start your server.
Let's see the basic structure:
const express = require('express');
const app = express();Handling HTTP GET Requests
Routes determine how your application responds to client requests to a specific endpoint (URL) and HTTP method (like GET, POST).
For a basic web page, we often use GET requests to retrieve data.
The app.get() method takes two arguments: the path (e.g., '/' for the homepage) and a callback function.
app.get('/', (req, res) => {
// Handle request and send response here
});Responding to Requests
Inside your route's callback function, you get two objects: req (request) and res (response).
The res object has methods to send responses back to the client. The simplest is res.send().
It can send various types of responses, like HTML, JSON, or plain text.
app.get('/', (req, res) => {
res.send('Hello from Express!');
});Making Your App Listen
Finally, your Express app needs to listen for incoming requests on a specific port.
The app.listen() method binds the application to a port on the local host. A common port for development is 3000.
Once running, you can access your app in a browser at http://localhost:3000.
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});Run Your First Express App!
Here's a complete, runnable example of a basic Express server. Save this as app.js and run it with node app.js.
Then, visit http://localhost:3000 in your browser!
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
res.send('Welcome to CoddyKit Express!');
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});More Endpoints!
Your API won't just have one page! You can define multiple routes for different paths.
Each app.get(), app.post(), etc., creates a new endpoint.
Try adding an /about route to your app.js file.
app.get('/about', (req, res) => {
res.send('This is the About page.');
});Express Server Quiz
Consider the following Express.js code snippet:
const express = require('express');
const app = express();
const PORT = 5000;
app.get('/', (req, res) => {
res.send('Home Page');
});
app.get('/api', (req, res) => {
res.send({ message: 'API data' });
});
app.listen(PORT, () => {
console.log(`Server started on port ${PORT}`);
});Express.js Foundations
Great job! You've learned the fundamentals of Express.js:
- What Express.js is and why it's useful.
- How to set up a Node.js project and install Express.
- Creating a basic Express application instance.
- Defining routes with
app.get(). - Sending responses with
res.send(). - Starting your server with
app.listen().
Next, we'll dive into more advanced routing and middleware concepts!
คำถามที่พบบ่อย
บทเรียน “พื้นฐานกรอบงาน Express.js” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “พื้นฐานกรอบงาน Express.js” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Node.js Backend Development Bootcamp ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Node.js Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “พื้นฐานกรอบงาน Express.js”
เริ่มต้นใช้งาน Express.js ตั้งค่าเซิร์ฟเวอร์พื้นฐาน และจัดการคำขอและการตอบกลับ HTTP คุณปฏิบัติ Node.js Backend Development Bootcamp ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Node.js Backend Development Bootcamp หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Node.js Backend Development Bootcamp บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “พื้นฐานกรอบงาน Express.js” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Node.js Backend Development Bootcamp นี้ได้ไหม
ได้ บทเรียน Node.js Backend Development Bootcamp ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- พื้นฐานกรอบงาน Express.js
- การกำหนดเส้นทางและมิดเดิลแวร์ใน Express
- การออกแบบจุดเชื่อมต่อ RESTful API
- การจัดการข้อมูลคำขอ: เนื้อหา คิวรี และพารามิเตอร์