node.js中實現(xiàn)GET和POST請求的代碼示例
創(chuàng)建基本的服務(wù)器
const express = require('express'); const indexRouter = require('./router'); // 引入路由 const app = express(); const port = 3000; // 掛載路由 app.use('/api', indexRouter); app.listen(port, () => { console.log(`Server is running on http://localhost:${port}`); });
創(chuàng)建路由文件
const express = require('express'); const router = express.Router(); module.exports = router;
實現(xiàn)GET請求
// 處理GET請求 router.get('/get', (req, res) => { // 通過 req.query 客戶端發(fā)送到服務(wù)器的數(shù)據(jù) const query = req.query; console.log(query, 'query') res.send({ code: 0, // 0: 請求成功 -1: 請求失敗 msg: 'GET請求成功', // 請求的狀態(tài)描述 data: query, // 服務(wù)器像客戶端返回數(shù)據(jù) }); });
實現(xiàn)POST請求
方式1:form-data,Express默認(rèn)不會解析form-data,因為它通常用于文件上傳,需要額外的處理。你可以使用multer這個中間件來處理multipart/form-data(也就是form-data)類型的請求。multer是專門為Express設(shè)計的,用于處理多部分/表單數(shù)據(jù),這包括上傳文件。
// 設(shè)置multer存儲選項(這里只是演示,實際上你可能需要配置磁盤存儲或其他選項) const storage = multer.memoryStorage(); const upload = multer({ storage: storage });
// 處理POST請求 router.post('/upload', upload.single('file'), (req, res) => { // 通過 req.query 客戶端發(fā)送到服務(wù)器的數(shù)據(jù) const body = req.body; console.log(body, 'body') res.send({ code: 0, // 0: 請求成功 -1: 請求失敗 msg: 'POST請求成功', // 請求的狀態(tài)描述 data: body, // 服務(wù)器像客戶端返回數(shù)據(jù) }); });
方式2:urlencoded,想要獲取url-encoded請求體的數(shù)據(jù),需要引入對應(yīng)的中間件。
// 配置解析表單數(shù)據(jù)的中間件 app.use(express.urlencoded({extended: false}))
// 處理POST請求 router.post('/post', (req, res) => { // 通過 req.query 客戶端發(fā)送到服務(wù)器的數(shù)據(jù) const body = req.body; console.log(body, 'body') res.send({ code: 0, // 0: 請求成功 -1: 請求失敗 msg: 'POST請求成功', // 請求的狀態(tài)描述 data: body, // 服務(wù)器像客戶端返回數(shù)據(jù) }); });
全部代碼
index.js
const express = require('express'); const indexRouter = require('./router'); // 引入路由 const app = express(); const port = 3000; // 配置解析表單數(shù)據(jù)的中間件 app.use(express.urlencoded({extended: false})) // 掛載路由 app.use('/api', indexRouter); // 啟動服務(wù)器 app.listen(port, () => { console.log(`Server is running on http://localhost:${port}`); });
router.js
const express = require('express'); const multer = require('multer'); const router = express.Router(); // 設(shè)置multer存儲選項(這里只是演示,實際上你可能需要配置磁盤存儲或其他選項) const storage = multer.memoryStorage(); // 使用內(nèi)存存儲,適用于小文件或不需要持久化的場景 const upload = multer({ storage: storage }); // 處理GET請求 router.get('/get', (req, res) => { // 通過 req.query 客戶端發(fā)送到服務(wù)器的數(shù)據(jù) const query = req.query; console.log(query, 'query') res.send({ code: 0, // 0: 請求成功 -1: 請求失敗 msg: 'GET請求成功', // 請求的狀態(tài)描述 data: query, // 服務(wù)器像客戶端返回數(shù)據(jù) }); }); // 處理POST請求 router.post('/upload', upload.single('file'), (req, res) => { // 通過 req.query 客戶端發(fā)送到服務(wù)器的數(shù)據(jù) const body = req.body; console.log(body, 'body') res.send({ code: 0, // 0: 請求成功 -1: 請求失敗 msg: 'POST請求成功', // 請求的狀態(tài)描述 data: body, // 服務(wù)器像客戶端返回數(shù)據(jù) }); }); // 處理POST請求 router.post('/post', (req, res) => { // 通過 req.query 客戶端發(fā)送到服務(wù)器的數(shù)據(jù) const body = req.body; console.log(body, 'body') res.send({ code: 0, // 0: 請求成功 -1: 請求失敗 msg: 'POST請求成功', // 請求的狀態(tài)描述 data: body, // 服務(wù)器像客戶端返回數(shù)據(jù) }); }); module.exports = router;
到此這篇關(guān)于node.js中實現(xiàn)GET和POST請求的代碼示例的文章就介紹到這了,更多相關(guān)node.js實現(xiàn)GET和POST請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nodejs創(chuàng)建簡易web服務(wù)器與文件讀寫的實例
下面小編就為大家?guī)硪黄猲ode js系列課程-創(chuàng)建簡易web服務(wù)器與文件讀寫的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09Node.js的Koa實現(xiàn)JWT用戶認(rèn)證方法
本篇文章主要介紹了Node.js的Koa實現(xiàn)JWT用戶認(rèn)證方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05node.js實現(xiàn)帶進(jìn)度條的多文件上傳
這篇文章主要為大家詳細(xì)介紹了node.js實現(xiàn)攜帶進(jìn)度條的多文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08Express+Nodejs 下的登錄攔截實現(xiàn)代碼
本篇文章主要介紹了Express+Nodejs 下的登錄攔截實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07在windows上用nodejs搭建靜態(tài)文件服務(wù)器的簡單方法
這篇文章主要介紹了在windows上用nodejs搭建靜態(tài)文件服務(wù)器的簡單方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08