Logo
Section 1 / 5

Express.js 概述:从 Node HTTP 到 Express 框架

了解为什么 Express.js 比 Node.js 的 HTTP 模块更受青睐,以及它如何简化后端开发。

导入

Express.js 概述:从 Node HTTP 到 Express 框架

了解为什么 Express.js 比 Node.js 的 HTTP 模块更受青睐,以及它如何简化后端开发。

No configuration found for ID: 7291fb195588acd49d5d56df4bbf161e350d1ec1dea3fae456f299bf21d0cb64

注意:点击“Run”按钮启动服务器,然后点击“+”标签页打开一个新终端。使用以下 curl 命令测试服务器端点。

  1. 访问根路由 (/):向服务器根路径发送一个 GET 请求。

    1curl http://localhost:3000/

    预期响应:

    1Welcome to our homepage!
  2. 访问关于页面 (/about):向 /about 路径发送一个 GET 请求。

    1curl http://localhost:3000/about

    预期响应:

    1About us page
  3. 访问不存在的页面 (/contact):向一个不存在的路由发送 GET 请求。

    1curl http://localhost:3000/contact

    预期响应:

    1Page not found

注意:点击“Run”按钮启动服务器,然后点击“+”标签页打开一个新终端。使用以下 curl 命令测试服务器端点。

  1. 访问根路由 (/):向服务器根路径发送一个 GET 请求。

    1curl http://localhost:3000/

    预期响应:

    1Welcome to our homepage!
  2. 访问关于页面 (/about):向 /about 路径发送一个 GET 请求。

    1curl http://localhost:3000/about

    预期响应:

    1About us page
  3. 访问不存在的页面 (/contact):向一个不存在的路由发送一个 GET 请求。

    1curl http://localhost:3000/contact

    预期响应:

    1Page not found

No configuration found for ID: 419acfb61d238d67cb347f44c651fd822a752a9e4ef0083d062beff337ff0aaf

注意:点击“Run”按钮启动服务器,然后点击“+”标签页打开一个新终端。使用以下 curl 命令测试服务器端点。

  1. 访问根路由 (/):向服务器根路径发送一个 GET 请求。

    1curl http://localhost:3000/

    预期响应:

    1Welcome to our homepage!
  2. 访问关于页面 (/about):向 /about 路径发送一个 GET 请求。

    1curl http://localhost:3000/about

    预期响应:

    1About us page
  3. 访问不存在的页面 (/contact):向一个不存在的路由发送 GET 请求。

    1curl http://localhost:3000/contact

    预期响应:

    1Page not found

注意:点击“Run”按钮启动服务器,然后点击“+”标签页打开一个新终端。使用以下 curl 命令测试服务器端点。

  1. 访问根路由 (/):向服务器根路径发送一个 GET 请求。

    1curl http://localhost:3000/

    预期响应:

    1Welcome to our homepage!
  2. 访问关于页面 (/about):向 /about 路径发送一个 GET 请求。

    1curl http://localhost:3000/about

    预期响应:

    1About us page
  3. 访问不存在的页面 (/contact):向一个不存在的路由发送一个 GET 请求。

    1curl http://localhost:3000/contact

    预期响应:

    1Page not found

No configuration found for ID: 4ee0bcc50481d48e9c7b955ec69ebc6d23b1c2cc62087f04c3400a5ff8a5f8dd

注意:点击“Run”按钮启动服务器,然后点击“+”标签页打开一个新终端。使用以下 curl 命令测试 Express 服务器端点。

  1. 访问根路由 (/):向服务器根路径发送一个 GET 请求。
    1curl http://localhost:3000/
    预期响应:
    1Hello, Express!

注意:点击“Run”按钮启动服务器,然后点击“+”标签页打开一个新终端。使用以下 curl 命令测试 Express 服务器端点。

  1. 访问根路由 (/):向服务器根路径发送一个 GET 请求。
    1curl http://localhost:3000/
    预期响应:
    1Hello, Express!

No configuration found for ID: 992cb8aa49edaf7e954858bc6ef4d431bf84b1d834d865ed1802517075c7e122

解决方案

解决方案

将以下代码粘贴到 server.js 文件中:

解决方案:创建一个 Express 服务器

1const express = require("express"); 2const app = express(); 3 4app.get("/welcome", (req, res) => { 5 res.send("Welcome to Express!"); 6}); 7 8app.listen(4000, () => { 9 console.log("Server running on port 4000"); 10});

解释:

  • 第 1 行:引入 Express 模块,该模块为构建 Node.js Web 应用提供了轻量且灵活的框架。

  • 第 2 行:创建一个 Express 应用实例,并将其存储在 app 变量中。

  • 第 4–6 行:定义一个位于 /welcome 的 GET 路由。当访问该端点时,服务器返回欢迎消息。

  • 第 8–10 行:启动 Express 服务器并监听 4000 端口。服务器启动后,会在控制台输出确认信息。

课程导引与准备工作

探索 Express.js 的请求-响应循环