
免費(fèi)API攔截工具入門指南:初學(xué)者必備教程與實(shí)用技巧
cd aws-lambda-express-demo
aws-lambda-express-demo
文件夾中創(chuàng)建一個(gè)文件 app.js
: touch app.js
npm
從注冊(cè)表下載最新版本的 express
包,并將其存儲(chǔ)在項(xiàng)目根目錄的 node_modules
文件夾中。所有的包依賴項(xiàng)也將安裝并存儲(chǔ)在該文件夾中: npm install express
serverless-http
的中間件框架,這是一個(gè)用于創(chuàng)建無(wú)服務(wù)器應(yīng)用程序的庫(kù)。AWS Lambda 允許正常編寫應(yīng)用程序,然后將其包裝在通過(guò) HTTP 請(qǐng)求導(dǎo)出和執(zhí)行的函數(shù)周圍。serverless-http
也與 Azure、Google Cloud 等無(wú)服務(wù)器提供商兼容: npm install serverless-http
如果需要,可以通過(guò)以下命令全局安裝 serverless-http
: npm install -g serverless-http
以下是使用 Node.js 實(shí)現(xiàn) GET
、POST
、DELETE
和 PUT
方法的 Express.js 框架實(shí)現(xiàn)的 RESTful API 示例:
const express = require('express');
const app = express();
const serverless = require('serverless-http');
const users = [
{ id: 1, name: 'John', company: "ABC Company" },
{ id: 2, name: 'Frank', company: "XYZ Inc." },
{ id: 3, name: 'Ashley', company: "123 Company" },
];
app.use(express.json());
app.get('/users', (req, res) => {
res.json(users);
});
app.get('/users/:id', (req, res) => {
const user = users.find(user => user.id === parseInt(req.params.id));
if (!user) return res.status(404).json({ message: 'User not found' });
res.json(user);
});
app.post('/users', (req, res) => {
const user = {
id: users.length + 1,
name: req.body.name,
company: req.body.company,
};
users.push(user);
res.json(user);
});
app.delete('/users/:id', (req, res) => {
const userIndex = users.findIndex(user => user.id === parseInt(req.params.id));
if (userIndex === -1) return res.status(404).json({ message: 'User not found' });
users.splice(userIndex, 1);
res.json({ message: 'User deleted' });
});
app.put('/users/:id', (req, res) => {
let user = users.find(user => user.id === parseInt(req.params.id));
if (!user) return res.status(404).json({ message: 'User not found' });
user.name = req.body.name;
user.company = req.body.company;
res.json(user);
});
const handler = serverless(app);
const startServer = async () => {
app.listen(3000, () => {
console.log("listening on port 3000!");
});
};
startServer();
module.exports.handler = (event, context, callback) => {
const response = handler(event, context, callback);
return response;
};
上述代碼創(chuàng)建了一個(gè) Express.js 應(yīng)用程序,并為 GET
、POST
、DELETE
和 PUT
方法添加了路由。應(yīng)用程序使用內(nèi)存中的 users
數(shù)組來(lái)存儲(chǔ)數(shù)據(jù),并使用 find
和 findIndex
方法根據(jù) URL 中提供的 ID 檢索和更新用戶。需要注意的是,對(duì)于 POST
和 PUT
路由,需要解析請(qǐng)求正文,這可以通過(guò) express.json()
中間件來(lái)完成。
在代碼的末尾,startServer
函數(shù)啟動(dòng)了本地服務(wù)器,而 module.exports.handler
函數(shù)將 Lambda 事件處理程序與 Express 應(yīng)用程序連接起來(lái)。
通過(guò)運(yùn)行以下命令來(lái)啟動(dòng)服務(wù)器:
node app.js
服務(wù)器啟動(dòng)后,API 將開(kāi)始運(yùn)行??梢允褂?Postman 發(fā)送測(cè)試 HTTP 請(qǐng)求。向 localhost:3000/users
發(fā)送請(qǐng)求時(shí),應(yīng)該會(huì)看到 200 OK
狀態(tài)代碼。此測(cè)試不需要請(qǐng)求正文。
原文鏈接:Building a RESTful API With AWS Lambda and Express
免費(fèi)API攔截工具入門指南:初學(xué)者必備教程與實(shí)用技巧
LLM內(nèi)容創(chuàng)作能力評(píng)估
使用Python語(yǔ)言調(diào)用零一萬(wàn)物API實(shí)戰(zhàn)指南
探索中國(guó)特色內(nèi)容知識(shí)的API合輯:構(gòu)建智慧應(yīng)用的橋梁
API 市場(chǎng)在 5 個(gè)領(lǐng)域中的作用
有道翻譯API:開(kāi)啟多語(yǔ)言交流新篇章
如何免費(fèi)調(diào)用手機(jī)歸屬地查詢API實(shí)現(xiàn)用戶定位
WordPress REST API 初學(xué)者指南
Python 輕松實(shí)現(xiàn)天氣提醒:通過(guò)天氣實(shí)況 API 獲取城市氣溫
對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力
一鍵對(duì)比試用API 限時(shí)免費(fèi)