扫一扫分享
环境
需要安装node
开始
1.新建项目 mkdir rest-api-demo && cd rest-api-demo
2.初始化项目 npm init
3.安装 npm install --save-dev json-server
4.在项目根目录下,新建一个 JSON 文件db.json
{
"posts": [
{ "id": 1, "title": "json-server", "author": "typicode" }
],
"comments": [
{ "id": 1, "body": "some comment", "postId": 1 }
],
"profile": { "name": "typicode" }
}
5.打开 package.json,在 scripts 字段添加一行
"scripts": {
"server": "json-server db.json", // 新加行
"test": "..."
}
6.启动服务npm run server
\{^_^}/ hi!
Loading db.json
Done
Resources
http://localhost:3000/posts
http://localhost:3000/comments
http://localhost:3000/profile
手机预览