概述:API意味着应用程序编程接口,简单来说,它的作用是允许应用程序相互通信。API是接收请求的信使,告诉系统您想要做什么,然后将响应返回给您!那么什么是Restful API呢?REST是Representational State传输的简称,它是一个在被调用时,服务器将所请求资源的状态表示传输给客户端的。
Node.js是一个基于Chrome的V8 JavaScript引擎构建的JavaScript运行时。您可以单击此处获取有关Node的更多信息。Express.js,或简称Express,是Node.js的Web应用程序框架。它旨在构建Web应用程序和API。它被称为Node.js最常被接受的服务器框架。
在我们使用它们构建简单的rest API时,请阅读上述每个软件包的文档,了解如何安装和使用它。
首先,创建一个文件夹可以调用任何你想要的名字。接下来,我们需要使用npm init - -y命令在该文件夹中创建一个package.json文件。接下来,在同一文件夹中也创建一个index.js文件,并将以下代码添加到该文件中。
const express = require('express'); const app = express(); const port = process.env.PORT || 3000; app.listen(port,()=>{ console.log('listening on port ' + port); })
在上面的代码中,我们基本上只是将express导入到我们的项目中,并分配我们希望服务器监听一个名为port的变量的端口,然后在我们的变量app上调用listen方法,这是一个express的实例。
接下来,我们创建一个JSON文件,其中包含我们将用于测试API端点的JSON数据。将下面的内容添加到您的JSON文件中,并将其命名为videodata.json
[ { "categoryID": "294", "parentID": "304", "subjectID": "7", "categoryName": "Apps and Side Dishes (Laura)", "categoryDescription": "Learn to make amazing appetizers and side dishes with Laura in the Kitchen.", "videosCount": "101", "forumCategoryID": "163" }, { "categoryID": "285", "parentID": "304", "subjectID": "7", "categoryName": "Side Dishes", "categoryDescription": "Side dish recipes for salads, vegetables, sauces with Hilah cooking.", "videosCount": "38", "forumCategoryID": "163" }, { "categoryID": "337", "parentID": "304", "subjectID": "7", "categoryName": "Side Dishes (bt)", "categoryDescription": "Side dish recipes with Byron Talbott.", "videosCount": "5", "forumCategoryID": "163" }, { "categoryID": "301", "parentID": "304", "subjectID": "7", "categoryName": "Side Dishes for Barbecue", "categoryDescription": "Barbecue side dish recipes done on the grill by the BBQ Pit Boys!", "videosCount": "43", "forumCategoryID": "163" }, { "categoryID": "297", "parentID": "304", "subjectID": "7", "categoryName": "Soups and Salads (Laura)", "categoryDescription": "Looking for the perfect recipe to start your meal? Or are you looking to eat something on the lighter side? These are sure to have you covered!", "videosCount": "70", "forumCategoryID": "163" } ]
接下来,我们必须在index.js文件中要求videodata.json文件,以便我们的API端点可以使用它。
app.use(express.json()); const data = require('./videodata.json'); //.= app.locals.videodata; app.get('/api/videos', (req,res) => { res.send(data); }) app.get('/api/videos/:categoryID', (req,res) => { const category = data.find((a) => a.categoryID == req.params.categoryID); if(!category){ return res.status(404).send('Category does not exist'); } return res.status(200).send(category); }) app.post('/api/videos', (req,res) => { const newInfo = { "categoryID": req.body.categoryID, "parentID": req.body.parentID, "subjectID": req.body.subjectID, "categoryName": req.body.categoryName, "categoryDescription": req.body.categoryDescription, "videosCount": req.body.videosCount, "forumCategoryID": req.body.forumCategoryID } data.push(newInfo); res.send(newInfo); })
在app.listen上方输入上面的代码。
屏幕截图中的URL称为端点。它们之前的get和post称为HTTP动词。使用第一个get方法,即
app.get(‘/api/videos’, (req,res) => { res.send(data); })
我们正在尝试检索videodata.json中的所有数据,而对于第二个get方法,我们尝试根据特定参数检索单个项目,在这种情况下是id。最后,使用post方法,我们尝试将新数据添加到对象数组中。
可以使用POSTMAN测试这些端点中的每一个。
我希望这可以帮助一些人开始使用Nodejs和Express。