现在已经是2018年了,Nodejs的天下,你还要依赖Java,Php,Go,Python,C#后台语言给你们提供RESTFul接口么,你就甘心拜在它们的脚下行走?不甘心的同鞋跟老哥走着。
这里dd给大家介绍一个款好用的工具,那就是json-server,目前在github
上热度上是这样子的
用一个表情来表示敬佩一个这样的项目
言归正传
接下来看看怎么使用
分为初级入坑篇,跟码农篇
环境准备
$ node --version
v8.7.0
$ npm --version
5.6.0
入坑篇(1)
初始化项目
mkdir test
cd test
npm init #请码哥们自己一路回车到世界末日
npm install json-server --save
创建一个db.json文件,里面保存你们的老婆列表
{
"girl-friends": [
{ "id": 1, "username": "西施"}
]
}
开始造老婆
json-server --watch db.json
列出你们的老婆 【查】
curl localhost:3000/girl-friends
[
{
"id": 1,
"username": "西施"
}
]
PS : ( 你以为我就那么一个功能么?哼 )
加老婆功能,用POST方法【增】
curl -X POST \
http://localhost:3000/girl-friends \
-H 'content-type: application/json' \
-d '{
"username":"凤姐"
}'
再列出你们的老婆,哈哈,凤姐入赘【查】
curl localhost:3000/girl-friends
[
{
"id": 1,
"username": "西施"
},
{
"id": 2,
"username": "凤姐"
}
]
跟老婆离婚(用DELETE方法,URL参数主要参数为ID编号)【删】
curl -X DELETE \
http://localhost:3000/girl-friends/1
剩下一个老婆太丑了,给她整一下容(用PUT方法)【改】
curl -X PUT \
http://localhost:3000/girl-friends/2 \
-H 'content-type: application/json' \
-d '{
"username":"表姐"
}'