Node.js 教程
Node.js 基础
Node.js 概述
Node.js 环境搭建
Node.js 基础语法
Node.js 模块
Node.js 异步编程
Node.js 核心模块
Node.js文件系统
Node.js路径模块(path)
Node.js的HTTP模块
Node.js的url模块
Node.js流模块(stream)
Node.js 常用工具与框架
npm 与包管理
Express 框架
数据库操作
Socket.io 与 WebSocket
Node.js 实战项目
Web 应用开发
API 开发
爬虫应用
自动化脚本
Node.js部署与优化
部署 Node.js 应用
Node.js性能优化
Node.js应用的安全性
Node.js 高级话题
Node.js中的设计模式
TypeScript 与 Node.js
GraphQL 与 API 开发
-
+
首页
Express 框架
### **Express 框架** 以下是对 Express 框架相关内容的详细说明: #### **1. Express 入门** - **什么是 Express**:Express 是一个轻量级的 Web 框架,用于构建 Node.js 应用程序。 - **安装 Express**: ```bash npm install express ``` - **创建第一个 Express 应用**: ```javascript const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => { res.send('Hello World!'); }); app.listen(port, () => { console.log(`Server is running at http://localhost:${port}`); }); ``` - **运行步骤**: 1. 保存文件为 `server.js`。 2. 在终端中运行 `node server.js`。 3. 打开浏览器并访问 `http://localhost:3000`,你将看到 "Hello World!"。 --- ### **2. 路由** - **定义路由**: ```javascript const express = require('express'); const app = express(); // GET 请求 app.get('/users', (req, res) => { res.send('GET request to /users'); }); // POST 请求 app.post('/users', (req, res) => { res.send('POST request to /users'); }); // 处理所有请求 app.all('/secret', (req, res) => { res.send('This is a secret page'); }); // 将多个路由组合到一个组中 const router = express.Router(); router.get('/profile', (req, res) => { res.send('Profile page'); }); router.get('/settings', (req, res) => { res.send('Settings page'); }); app.use('/account', router); app.listen(3000, () => { console.log('Server is running at http://localhost:3000'); }); ``` --- ### **3. 中间件** - **什么是中间件**:中间件是 Express 应用程序的构建块,它有访问请求和响应对象,以及应用程序请求的生命周期中的下一个中间件函数的能力。 - **使用中间件**: ```javascript // 自定义中间件 const loggerMiddleware = (req, res, next) => { console.log(`Request received at ${new Date()}`); next(); }; app.use(loggerMiddleware); // Express 内置中间件 app.use(express.json()); // 解析 JSON 数据 app.use(express.urlencoded({ extended: true })); // 解析 URL 编码数据 ``` --- ### **4. 文件上传** - **使用 multer 中间件**: ```javascript const express = require('express'); const multer = require('multer'); const app = express(); const upload = multer({ dest: 'uploads/' }); // 上传文件的存储路径 app.post('/upload', upload.single('file'), (req, res) => { if (!req.file) { return res.status(400).send('No file uploaded.'); } res.send(`File uploaded to ${req.file.path}`); }); app.listen(3000, () => { console.log('Server is running at http://localhost:3000'); }); ``` --- ### **5. HTTPS 服务器** - **生成自签名证书**: ```bash openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 ``` - **创建 HTTPS 服务器**: ```javascript const express = require('express'); const app = express(); const https = require('https'); const fs = require('fs'); // 加载证书和私钥 const options = { key: fs.readFileSync('key.pem'), cert: fs.readFileSync('cert.pem') }; app.get('/', (req, res) => { res.send('Hello over HTTPS!'); }); https.createServer(options, app).listen(443, () => { console.log('HTTPS server is running on port 443'); }); ``` ### **总结** Express 是一个功能强大且灵活的 Web 框架,简化了 HTTP 请求处理、路由管理和模板渲染等任务。通过使用中间件和各种工具,可以轻松实现复杂的功能,如文件上传和 HTTPS 服务器。如果你需要进一步扩展功能,可以参考 Express 的官方文档:[http://expressjs.com/](http://expressjs.com/)。
wwbang
2025年2月26日 18:25
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码