郑州中企业网站建设做淘宝要网站

当前位置: 首页 > news >正文

郑州中企业网站建设,做淘宝要网站,网站维护的要求,wordpress网络科技公司模板简介 json-server 是一款小巧的接口模拟工具#xff0c;一分钟内就能搭建一套 Restful 风格的 API#xff0c;尤其适合前端接口测试使用。 只需指定一个 json 文件作为 api 的数据源即可#xff0c;使用起来非常方便#xff0c;30秒入门#xff0c;基本上有手就行。 进阶…简介 json-server 是一款小巧的接口模拟工具一分钟内就能搭建一套 Restful 风格的 API尤其适合前端接口测试使用。 只需指定一个 json 文件作为 api 的数据源即可使用起来非常方便30秒入门基本上有手就行。 进阶操作还支持分页排序等操作简直强大。 开源地址 主页地址json-server - npm Github项目地址GitHub - typicode/json-server: Get a full fake REST API with zero coding in less than 30 seconds (seriously) 30秒入门 环境依赖 安装 Node.js 环境即可 操作步骤 安装 JSON 服务器 npm install -g json-server创建一个db.json包含一些数据的文件 {posts: [{ id: 1, title: json-server, author: typicode }],comments: [{ id: 1, body: some comment, postId: 1 }],profile: { name: typicode } }启动 json-server 接口服务器 json-server –watch db.json浏览器访问 http://localhost:3000/posts/1你会得到 { id: 1, title: json-server, author: typicode }补充 如果您发出 POST、PUT、PATCH 或 DELETE 请求更改将自动安全地保存到 db.json 文件中。注意 id 值是不可变的。 路由进阶 根据之前的db.json文件这里是所有的默认路由。 路由形式一 GET /posts GET /posts/1 POST /posts PUT /posts/1 PATCH /posts/1 DELETE /posts/1路由形式二 GET /profile POST /profile PUT /profile PATCH /profile筛选 使用 . 访问筛选 GET /posts?titlejson-serverauthortypicode GET /posts?id1id2 GET /comments?author.nametypicode分页 使用_page和可选地_limit对返回的数据进行分页。 在Link标题你会得到firstprevnext和last链接。 GET /posts?_page7 GET /posts?_page7_limit20默认返回10项 排序 添加_sort和_order默认升序 GET /posts?_sortviews_orderasc GET /posts/1/comments?_sortvotes_orderasc对于多个字段请使用以下格式 GET /posts?_sortuser,views_orderdesc,asc切片(分页) 添加_start和_end或_limit GET /posts?_start20_end30 GET /posts/1/comments?_start20_end30 GET /posts/1/comments?_start20_limit10与Array.slice完全一样工作即_start开始_end结束 特殊符号 添加_gte或_lte获取范围 GET /posts?views_gte10views_lte20添加_ne以排除值 GET /posts?id_ne1添加_like到过滤器支持正则表达式 GET /posts?title_likeserver全文搜索 添加 q GET /posts?qinternet关系 要包含子资源请添加 _embed GET /posts?_embedcomments GET /posts/1?_embedcomments要包含父资源请添加 _expand GET /comments?_expandpost GET /comments/1?_expandpost获取或创建嵌套资源默认为一级 GET /posts/1/comments POST /posts/1/comments数据库 GET /db主页 返回默认索引文件或服务./public目录 GET /附加功能 静态文件服务器 您可以使用 JSON Server 为您的 HTML、JS 和 CSS 提供服务只需创建一个./public目录或用于–static设置不同的静态文件目录。 mkdir public echo hello world public/index.html json-server db.jsonjson-server db.json –static ./some-other-dir替换端口 您可以使用以下–port标志在其他端口上启动 JSON Server  \( json-server --watch db.json --port 3004支持跨域 您可以使用 CORS 和 JSONP 从任何地方访问您模拟的 API 接口。 远程模式 您可以加载远程模式。 \) json-server http://example.com/file.json \( json-server http://jsonplaceholder.typicode.com/db生成随机数据 使用 JS 而不是 JSON 文件您可以通过编程方式创建数据。 // index.js module.exports () {const data { users: [] }// 创建 1000 个用户信息for (let i 0; i 1000; i) {data.users.push({ id: i, name: user\){i} })}return data }\( json-server index.js提示使用Faker、Casual、Chance或JSON Schema Faker 等模块。 添加自定义路由 创建一个routes.json文件。注意每条路线都以/. {/api/*: /\)1,/:resource/:id/show: /:resource/:id,/posts/:category: /posts?category:category,/articles\?id:id: /posts/:id }使用–routes选项启动 JSON 服务器。 json-server db.json –routes routes.json现在您可以使用其他路线访问资源。 /api/posts # → /posts /api/posts/1 # → /posts/1 /posts/1/show # → /posts/1 /posts/javascript # → /posts?categoryjavascript /articles?id1 # → /posts/1添加中间件 您可以使用以下–middlewares选项从 CLI 添加中间件 // hello.js module.exports (req, res, next) {res.header(X-Hello, World)next() }json-server db.json –middlewares ./hello.js json-server db.json –middlewares ./first.js ./second.js命令行使用 json-server [options] sourceOptions:–config, -c Path to config file [default: json-server.json]–port, -p Set port [default: 3000]–host, -H Set host [default: localhost]–watch, -w Watch file(s) [boolean]–routes, -r Path to routes file–middlewares, -m Paths to middleware files [array]–static, -s Set static files directory–read-only, –ro Allow only GET requests [boolean]–no-cors, –nc Disable Cross-Origin Resource Sharing [boolean]–no-gzip, –ng Disable GZIP Content-Encoding [boolean]–snapshots, -S Set snapshots directory [default: .]–delay, -d Add delay to responses (ms)–id, -i Set database id property (e.g. _id) [default: id]–foreignKeySuffix, –fks Set foreign key suffix, (e.g. _id as in post_id)[default: Id]–quiet, -q Suppress log messages from output [boolean]–help, -h Show help [boolean]–version, -v Show version number [boolean]Examples:json-server db.jsonjson-server file.jsjson-server http://example.com/db.jsonhttps://github.com/typicode/json-server您还可以在json-server.json配置文件中设置选项。 {port: 3000 }模块 如果您需要添加身份验证、验证或任何行为您可以将项目作为模块与其他 Express 中间件结合使用。 简单的例子 \( npm install json-server --save-dev// server.js const jsonServer require(json-server) const server jsonServer.create() const router jsonServer.router(db.json) const middlewares jsonServer.defaults()server.use(middlewares) server.use(router) server.listen(3000, () {console.log(JSON Server is running) })\) node server.js您提供给jsonServer.router函数的路径是相对于您启动节点进程的目录的。如果从另一个目录运行上述代码最好使用绝对路径 const path require(path) const router jsonServer.router(path.join(__dirname, db.json))对于内存数据库只需将对象传递给jsonServer.router(). 另请注意jsonServer.router()它可用于现有的 Express 项目。 自定义路由示例 假设您想要一个回显查询参数的路由和另一个在创建的每个资源上设置时间戳的路由。 const jsonServer require(json-server) const server jsonServer.create() const router jsonServer.router(db.json) const middlewares jsonServer.defaults()// 设置默认中间件记录器、静态、cors 和无缓存 server.use(middlewares)// 写在自定义路由之前 server.get(/echo, (req, res) {res.jsonp(req.query) })// 要处理 POST、PUT 和 PATCH您需要使用 body-parser // 您可以使用 JSON Server server.use(jsonServer.bodyParser) server.use((req, res, next) {if (req.method POST) {req.body.createdAt Date.now()}// 继续到 JSON Server 路由器next() })// 使用默认路由器 server.use(router) server.listen(3000, () {console.log(JSON Server is running) })访问控制示例 const jsonServer require(json-server) const server jsonServer.create() const router jsonServer.router(db.json) const middlewares jsonServer.defaults()server.use(middlewares) server.use((req, res, next) {if (isAuthorized(req)) { // 在此处添加您的授权逻辑next() // 继续到 JSON Server 路由器} else {res.sendStatus(401)} }) server.use(router) server.listen(3000, () {console.log(JSON Server is running) })自定义输出示例 要修改响应请覆盖router.render方法 // 在这个例子中返回的资源将被包装在一个 body 属性 router.render (req, res) {res.jsonp({body: res.locals.data}) }您可以为响应设置自己的状态代码 // 在这个例子中我们模拟了一个服务器端错误响应 router.render (req, res) {res.status(500).jsonp({error: error message here}) }重写器示例 要添加重写规则请使用jsonServer.rewriter() // 写在 server.use(router) 之前 server.use(jsonServer.rewriter({/api/*: /$1,/blog/:resource/:id/show: /:resource/:id }))在另一个端点上挂载 JSON 服务器示例 或者您也可以将路由器安装在/api. server.use(/api, router)API jsonServer.create() 返回一个 Express 服务器。 jsonServer.defaults([options]) 返回 JSON 服务器使用的中间件。 选项 static 静态文件的路径logger 启用记录器中间件默认值truebodyParser 启用 body-parser 中间件默认值truenoCors 禁用 CORS默认值falsereadOnly 只接受 GET 请求默认值false jsonServer.router([path|object]) 返回 JSON 服务器路由器。