使用 NodeJS + Express 從 GET/POST Request 取值(Nodejs相关)
使用 NodeJS + Express 從 GET/POST Request 取值(Nodejs相关)
過去無論哪一種網站應用程式的開發語言,初學者教學中第一次會提到的起手式,八九不離十就是 GET/POST Request 的取值。但是,在 Node.js + Express 的世界中,彷彿人人是高手,天生就會使用,從不曾看到有人撰文說明。
這應該算是開發 Web Service 的入門,在 Client 與 Server 的互動中,瀏覽器發出 GET/POST Request 時會傳值給 Server-side,常見應用就是網頁上以 POST method 送出的表單內容,或是網址列上的 Query Strings (ex: page?page=3&id=5)。然後,我們的網站應用程式透過解析這些參數,得到使用者上傳的資訊。
取得 GET Request 的 Query Strings:
GET /test?name=fred&tel=0926xxx572
app.get(’/test’, function(req, res) {
console.log(req.query.name);
console.log(req.query.tel);
});
如果是透過表單且是用 POST method:
<form action='/test' method='post'>
<input type='text' name='name' value='fred'>
<input type='text' name='tel' value='0926xxx572'>
<input type='submit' value='Submit'>
</form>
app.post('/test', function(req, res) {
console.log(req.query.id);
console.log(req.body.name);
console.log(req.body.tel);
});
當然也可以 Query Strings 和 POST method 的表單同時使用:
<form action='/test?id=3' method='post'>
<input type='text' name='name' value='fred'>
<input type='text' name='tel' value='0926xxx572'>
<input type='submit' value='Submit'>
</form>
app.post('/test', function(req, res) {
console.log(req.query.id);
console.log(req.body.name);
console.log(req.body.tel);
});
順帶補充,還有另一種方法傳遞參數給 Server,就是使用路徑的方式,可以利用 Web Server 的 HTTP Routing 來解析,常見於的各種 Web Framework。這不算是傳統標準規範的做法,是屬於 HTTP Routing 的延伸應用。
GET /hello/fred/0926xxx572
app.get('/hello/:name/:tel', function(req, res) {
console.log(req.params.name);
console.log(req.params.tel);
});
使用 NodeJS + Express 從 GET/POST Request 取值
在 Node.js + Express 中,處理 HTTP 請求(包括 GET 和 POST 方法)並從請求中提取數據是非常基礎的操作。下面將詳細介紹如何在 Express 中處理 GET 和 POST 請求,並從請求中獲取相應的參數。
GET 請求
當瀏覽器通過 URL 發送 GET 請求時,通常會包含查詢字符串(Query String),例如 http://example.com/test?name=fred&tel=0926xxx572
。這些查詢字符串可以通過 req.query
來訪問。
// 定義一個 GET 路由
app.get('/test', function(req, res) {
// 輸出查詢字符串中的 name 和 tel
console.log(req.query.name); // 输出 "fred"
console.log(req.query.tel); // 输出 "0926xxx572"
// 返回響應
res.send('GET 请求处理成功');
});
POST 請求
當用戶通過 HTML 表單提交 POST 請求時,表單中的數據會被發送到指定的 URL。這些數據可以通過 req.body
來訪問。需要注意的是,要使用 body-parser
中間件來解析請求體中的數據。
首先,需要安裝 body-parser
:
npm install body-parser
然後,在應用程序中引入並使用它:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
// 使用 bodyParser 解析请求体
app.use(bodyParser.urlencoded({ extended: true }));
// 定义一个 POST 路由
app.post('/test', function(req, res) {
// 输出表单数据中的 name 和 tel
console.log(req.body.name); // 输出 "fred"
console.log(req.body.tel); // 输出 "0926xxx572"
// 返回响应
res.send('POST 请求处理成功');
});
// 启动服务器
app.listen(3000, () => {
console.log('服务器启动在 http://localhost:3000');
});
同時使用 GET 請求和 POST 請求
有時候,你可能會需要同時使用 GET 請求中的查詢字符串和 POST 請求中的表單數據。這種情況下,你可以像上面一樣定義路由並從 req.query
和 req.body
分別獲取數據。
app.post('/test', function(req, res) {
// 输出查询字符串中的 id
console.log(req.query.id); // 输出 "3"
// 输出表单数据中的 name 和 tel
console.log(req.body.name); // 输出 "fred"
console.log(req.body.tel); // 输出 "0926xxx572"
// 返回响应
res.send('请求处理成功');
});
使用路徑參數
除了查詢字符串外,還可以通過 URL 路徑傳遞參數,這種方式通常在 Web 框架中使用。例如,GET /hello/fred/0926xxx572
。
app.get('/hello/:name/:tel', function(req, res) {
// 輸出路徑参数中的 name 和 tel
console.log(req.params.name); // 输出 "fred"
console.log(req.params.tel); // 输出 "0926xxx572"
// 返回响应
res.send('路径参数请求处理成功');
});
以上就是使用 Node.js + Express 處理 GET 和 POST 請求並從請求中提取數據的基本方法。希望這些示例代碼能幫助你更好地理解這個過程。
是啊. 入门的文章太少了, 摸了好久的
别忘了 app.use(express.bodyParser());
总结一下,是不是应该就三种:
- req.params.xxxxx 从path中的变量
- req.query.xxxxx 从get中的?xxxx=中
- req.body.xxxxx 从post中的变量
我也是这么认为的
express 还可以 req.param(“id”) GET/POST都适用吧?
欢迎补充,是的没有问题的,刚试过三种方式的参数都可以 req.param() 这样得到
支持楼主
mark
mark
楼主台湾人?
在 Node.js + Express 中,获取 GET 和 POST 请求的参数是非常常见的任务。以下是如何从 GET 和 POST 请求中获取参数的详细示例。
获取 GET 请求中的 Query Strings
当你通过 URL 发送请求时,可以在 URL 后面添加查询字符串(Query Strings),Express 可以轻松地从 req.query
对象中获取这些值。
// app.js
const express = require('express');
const app = express();
// 处理 GET 请求
app.get('/test', (req, res) => {
const name = req.query.name;
const tel = req.query.tel;
console.log(name); // 输出 'fred'
console.log(tel); // 输出 '0926xxx572'
res.send(`Name: ${name}, Tel: ${tel}`);
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
获取 POST 请求中的表单数据
对于 POST 请求,通常是从表单提交的数据。你需要使用 body-parser
中间件来解析请求体中的 JSON 或 URL 编码数据。
首先安装 body-parser
:
npm install body-parser
然后在你的应用中使用它:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
// 处理 POST 请求
app.post('/test', (req, res) => {
const id = req.query.id; // 如果你在 form action 中使用了 query string
const name = req.body.name;
const tel = req.body.tel;
console.log(id); // 输出 '3'
console.log(name); // 输出 'fred'
console.log(tel); // 输出 '0926xxx572'
res.send(`ID: ${id}, Name: ${name}, Tel: ${tel}`);
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
使用路径参数
除了通过查询字符串传递参数,还可以通过 URL 路径传递参数。
// app.js
const express = require('express');
const app = express();
// 处理带路径参数的 GET 请求
app.get('/hello/:name/:tel', (req, res) => {
const name = req.params.name;
const tel = req.params.tel;
console.log(name); // 输出 'fred'
console.log(tel); // 输出 '0926xxx572'
res.send(`Hello ${name}, your phone number is ${tel}`);
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
这样,你就可以轻松地处理来自客户端的各种类型的数据了。