
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
在前端开发中,您可能遇到这样的情景:
项目讨论会后
经理:好,开发时间就定为两周
服务端:OK,没问题,两周能搞定
前端:干,两周我也能搞定,可尼玛我要等服务端接口啊
然后,这两周期间,你不断轮询服务端人员,催促接口。到最后,服务端如期完成,作为前端的你还在继续撸...
[mock-api][ 就是为了解决前端等待服务端开发接口而产生的一个小工具,使用json文件模拟服务端请求结果。工作流程编程了这样:
项目讨论会后
经理:好,开发时间就定为两周
服务端:OK,没问题,两周能搞定
前端:OK,服务端先定义好接口文档,然后大家一起过一下文档
经理:这是必须的
然后服务端定义了接口文档,大家通过后,前端利用mock-api模拟和服务端并行开发
两周后,项目开发如期完成
使用mock-api,需要具备:
npm install -g nodemon
即可npm install -g mock-api
即可假设服务端约好了一堆API,我们拿其中一个来示例:
请求用户详情
GET: /users/:id
返回:
{
"id": 用户id,
"name": "用户名",
"level": 用户等级
}
新建一个目录,比如取做:restful。在该目录下建立一个json文件,比如:user.json,写入以下内容:
[
{
"method": "get",
"url": "/users/:id",
"response": {
"id": 1,
"name": "tom",
"level": 3
}
}
]
然后执行:
mock-api serve /path/to/restful
上面的 /path/to/restful
是之前建立的restful目录的路径,可以是相对地址,也可以是绝对地址。
前端开发便可以通过 http://localhost:3001/users/1 访问到该接口。mock-api已经做了跨域处理,因此不存在跨域问题。
如果需要定制端口,启动命令使用:
mock-api serve /path/to/restful -p port
注意,为了便于开发,建议将上面的 http://localhost:3001
进行配置,后期接入真实API则只需要修改一处配置即可。
FAQs
use json files to mock apis for front-end development
We found that apis-mock demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.