什么是JSON Server
JSON Server是基于nodejs的静态API框架,能将本地静态文件快速映射成静态的REST API。
至于什么是REST API?请参考廖雪峰老师的网站上这篇解释,非常的通俗易懂。https://www.liaoxuefeng.com/wiki/1022910821149312/1105003357927328
快速上手
- 安装json-server
npm install -g json-server #全局安装json-server
- 新建一个db.json
1 { 2 "articles":[ 3 { 4 "id":1, 5 "title":"JSP JavaBean", 6 "url":"https://www.runoob.com/jsp/jsp-javabean.html", 7 "comments":[ 8 { 9 "id":1, 10 "user":"Peter", 11 "context":"good article!" 12 },{ 13 "id":2, 14 "user":"Tom", 15 "context":"nice points!" 16 } 17 ] 18 }, 19 { 20 "id":2, 21 "title":"what's REST API?", 22 "url":"https://www.liaoxuefeng.com/wiki/1022910821149312/1105003357927328", 23 "comments":[ 24 { 25 "id":1, 26 "user":"Mary", 27 "context":"valuable article!" 28 },{ 29 "id":2, 30 "user":"Jack", 31 "context":"not bad!" 32 } 33 ] 34 } 35 ], 36 "menu":[ 37 { 38 "id":1, 39 "menu_name":"MainPage" 40 },{ 41 "id":2, 42 "menu_name":"Java" 43 } 44 ] 45 }
- 启动json-server服务
json-server --watch db.json
//出现以下信息标识成功
/**{^_^}/ hi!
Loading db.json
DoneResources
http://localhost:3000/articles
http://localhost:3000/menuHome
http://localhost:3000Type s + enter at any time to create a snapshot of the database
Watching...*/