参考自
REST是设计风格而不是标准。REST通常基于使用HTTP,URI,和XML(标准通用标记语言下的一个子集)以及HTML(标准通用标记语言下的一个应用)这些现有的广泛流行的协议和标准。REST 通常使用 JSON 数据格式。
以下为 REST 基本架构的四个方法:
{
"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
}
}
URI | HTTP方法 | 发送内容 | 结果 |
---|---|---|---|
listUsers | GET | 空 | 显示所有用户列表 |
addUser | POST | JSON字符串 | 添加新用户 |
deleteUser | DELETE | JSON字符串 | 删除用户 |
:id | GET | 空 | 显示用户纤细信息 |
.
├── index.html
├── node_modules
├── package.json
├── package-lock.json
├── server.js
└── users.json
user.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}}
index.html
<html>
<body>
<p>Add A User</p>
<form action="http://127.0.0.1:8888/addUser" method="POST">
Name: <input type="text" name="name"> <br>
Password: <input type="text" name="password"> <br>
Profession: <input type="text" name="profession"><br>
ID: <input type="number" name="id" min="0"><br>
<input type="submit" value="Submit">
</form>
<p>Delete A User</p>
<form action="http://127.0.0.1:8888/deleteUser" method="POST">
ID: <input type="number" name="id" min="0"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
server.js
var express = require('express');
var app = express();
var fs = require('fs');
var bodyParser = require('body-parser');
// 创建 application/x-www-form-urlencoded 编码解析
var urlencodedParser = bodyParser.urlencoded({ extended: false })
app.get('/', function (req, res) {
res.sendFile(__dirname + '/' + 'index.html');
})
app.get('/listUsers', function (req, res) {
fs.readFile(__dirname + '/' + 'users.json', 'utf8', function (err, data) {
// 返回的是json
// 更多见https://www.cnblogs.com/digdeep/p/5574366.html
res.setHeader('Content-Type', 'application/json; charset=utf8');
res.end(data);
});
})
app.post('/addUser', urlencodedParser, function (req, res) {
// 输出 JSON 格式
var newUser = {
'name' : req.body.name,
'password' : req.body.password,
'profession' : req.body.profession,
'id': parseInt(req.body.id)
};
fs.readFile(__dirname + '/' + 'users.json', 'utf8', function (err, data) {
if(err) {
console.error(err);
return;
}
data = JSON.parse(data);
data['user' + newUser['id']] = newUser;
fs.writeFile(__dirname + '/' + 'users.json', JSON.stringify(data), 'utf8', function (err) {
if(err) {
console.error(err);
return
}
res.setHeader('Content-Type', 'application/json; charset=utf8');
res.end(JSON.stringify(newUser));
});
});
})
app.post('/deleteUser', urlencodedParser, function (req, res) {
// 输出 JSON 格式
res.setHeader('Content-Type', 'application/json; charset=utf8');
var retMessage = 'Fail to delete!';
fs.readFile(__dirname + '/' + 'users.json', 'utf8', function (err, data) {
if(err) {
console.error(err);
return;
}
data = JSON.parse(data);
var deleteUser = data['user' + parseInt(req.body.id)];
delete data['user' + parseInt(req.body.id)];
fs.writeFile(__dirname + '/' + 'users.json', JSON.stringify(data), 'utf8', function (err) {
if(err) {
console.error(err);
return
}
res.setHeader('Content-Type', 'application/json; charset=utf8');
res.end(JSON.stringify(deleteUser));
});
});
})
app.get('/:id', function (req, res) {
fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
data = JSON.parse( data );
var user = data["user" + req.params.id]
res.setHeader('Content-Type', 'application/json; charset=utf8');
res.end( JSON.stringify(user));
});
})
var server = app.listen(8888, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Server is running in http://%s:%s', host, port);
})