Node.js RESTful API
-
-
HTTP方法
在基于REST的体系结构中,通常使用以下四种HTTP方法。- GET-用于提供对资源的只读访问。
- PUT-用于创建新资源。
- DELETE-用于删除资源。
- POST-用于更新现有资源或创建新资源。
-
RESTful Web服务
-
为库创建RESTful
考虑我们有一个基于JSON的用户数据库,在文件users.json中具有以下用户:{ "user1" : { "name" : "mahesh", "password" : "password1", "profession" : "teacher", "id": 1 }, "user2" : { "name" : "suresh", "password" : "password2", "profession" : "librarian", "id": 2 }, "user3" : { "name" : "ramesh", "password" : "password3", "profession" : "clerk", "id": 3 } }
基于此信息,我们将提供以下RESTful API。URI HTTP方法 请求体 结果 listUsers GET NULL 显示所有用户的列表。 addUser POST JSON字串 添加新用户的详细信息。 deleteUser DELETE JSON字串 删除现有用户。 :id GET NULL 显示用户的详细信息。 假设您已经知道如何使用Ajax或简单表单数据从前端传递值以及如何使用Express Request对象处理它们,我将以硬编码的形式保留所有示例的大部分内容。 -
listUsers
让我们在server.js文件中使用以下代码实现第一个RESTful API listUsers:var express = require('express'); var app = express(); var fs = require("fs"); app.get('/listUsers', function (req, res) { fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) { console.log( data ); res.end( data ); }); }) var server = app.listen(8081, function () { var host = server.address().address var port = server.address().port console.log("Example app listening at http://%s:%s", host, port) })
现在尝试使用URL访问已定义的API: http://127.0.0.1:8081/listUsers和HTTP方法:使用任何REST客户端在本地计算机上获取。这应该产生以下结果-将解决方案投入生产环境时,可以更改给定的IP地址。{ "user1" : { "name" : "mahesh", "password" : "password1", "profession" : "teacher", "id": 1 }, "user2" : { "name" : "suresh", "password" : "password2", "profession" : "librarian", "id": 2 }, "user3" : { "name" : "ramesh", "password" : "password3", "profession" : "clerk", "id": 3 } }
-
addUser
以下API将向您展示如何在列表中添加新用户。以下是新用户的详细信息-user = { "user4" : { "name" : "mohit", "password" : "password4", "profession" : "teacher", "id": 4 } }
您可以使用Ajax调用以JSON形式接受相同的输入,但是出于教学的观点,我们在这里对其进行了硬编码。以下是数据库中新用户的addUser API-var express = require('express'); var app = express(); var fs = require("fs"); var user = { "user4" : { "name" : "mohit", "password" : "password4", "profession" : "teacher", "id": 4 } } app.post('/addUser', function (req, res) { // First read existing users. fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) { data = JSON.parse( data ); data["user4"] = user["user4"]; console.log( data ); res.end( JSON.stringify(data)); }); }) var server = app.listen(8081, function () { var host = server.address().address var port = server.address().port console.log("Example app listening at http://%s:%s", host, port) })
现在,尝试使用URL访问定义的API :http://127.0.0.1:8081/addUser和HTTP方法:使用任何REST客户端在本地计算机上进行POST。这应该产生以下结果-此方法用于清除按名称指定的cookie。以下是一些示例-{ "user1":{"name":"mahesh","password":"password1","profession":"teacher","id":1}, "user2":{"name":"suresh","password":"password2","profession":"librarian","id":2}, "user3":{"name":"ramesh","password":"password3","profession":"clerk","id":3}, "user4":{"name":"mohit","password":"password4","profession":"teacher","id":4} }
-
:id
现在,我们将实现一个API,该API将使用用户ID进行调用,并显示相应用户的详细信息。var express = require('express'); var app = express(); var fs = require("fs"); app.get('/:id', function (req, res) { // First read existing users. fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) { var users = JSON.parse( data ); var user = users["user" + req.params.id] console.log( user ); res.end( JSON.stringify(user)); }); }) var server = app.listen(8081, function () { var host = server.address().address var port = server.address().port console.log("Example app listening at http://%s:%s", host, port) })
现在尝试使用URL访问定义的API :http://127.0.0.1:8081/2和HTTP方法:使用任何REST客户端在本地计算机上GET。这应该产生以下结果-{"name":"suresh","password":"password2","profession":"librarian","id":2}
-
deleteUser
此API与addUser API非常相似,在addUser API中,我们通过req.body接收输入数据,然后根据用户ID从数据库中删除该用户。为了简化程序,我们假设要删除ID为2的用户。var express = require('express'); var app = express(); var fs = require("fs"); var id = 2; app.delete('/deleteUser', function (req, res) { // First read existing users. fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) { data = JSON.parse( data ); delete data["user" + 2]; console.log( data ); res.end( JSON.stringify(data)); }); }) var server = app.listen(8081, function () { var host = server.address().address var port = server.address().port console.log("Example app listening at http://%s:%s", host, port) })
现在尝试使用URL访问定义的API :http://127.0.0.1:8081/deleteUser和HTTP方法:使用任何REST客户端在本地计算机上DELETE。这应该产生以下结果-{"user1":{"name":"mahesh","password":"password1","profession":"teacher","id":1},"user3":{"name":"ramesh","password":"password3","profession":"clerk","id":3}}