标签: json-server

Json-server的Rest API问题

我想要json-server的test rest API.首先我用npm安装json-server,然后用以下数据创建db.json:

[
    {
        "id": 1,
        "name": "Angular Connect",
        "date": "2036-09-25T23:00:00.000Z",
        "time": "10:00 am",
        "price": 599.99,
        "imageUrl": "/assets/images/angularconnect-shield.png",
        "location": {
            "address": "1057 DT",
            "city": "London",
            "country": "England"
        },
        "sessions": [
            {
                "id": 1,
                "name": "Using Angular 4 Pipes",
                "presenter": "Peter Bacon Darwin",
                "duration": 1,
                "level": "Intermediate",
                "abstract": "Learn all about the new pipes in Angular 4, both \n        how to write them, and how to get the new AI CLI to write \n        them for you. Given by the …
Run Code Online (Sandbox Code Playgroud)

node.js json-server angular

2
推荐指数
1
解决办法
577
查看次数

使用 json-server 模拟搜索过滤器

我有routes.jsondb.json

路线

  "/api/*/_search?*=:searchstring": "/$1/?$2_like=:searchstring",
  "/api/*": "/$1"
Run Code Online (Sandbox Code Playgroud)

数据库文件

 {
  "cats": {
    "cats": []
  },
  "bats": [],
  "recordList": {
    "records": [
      {id:1, name: 'abc'},
      {id:2, name: 'def'},
      {id:3, name: 'ghi'}
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

使用上述配置获取记录列表绝对没问题。

需要了解如何模拟下面的搜索过滤器调用:

http:localhost:3001/api/_search?name=abc
Run Code Online (Sandbox Code Playgroud)

将路由更新为:

{
  "/api/*": "/$1",
  "/api/_search?name_like": "/$1"
}
Run Code Online (Sandbox Code Playgroud)

按照此链接:https : //github.com/typicode/json-server/issues/654#issuecomment-339098881

但是没有点击定义的配置 URL,我做错了什么?我在这里错过了什么吗?搜索词是动态的,因此传递的值应该只能从变量中接受,但在注释中它是静态的。如果有人遇到类似问题并已解决,请对此提供帮助

如果搜索 'abc',它应该返回

{
  records: [{id: 1, name: 'abc'}]
}
Run Code Online (Sandbox Code Playgroud)

json mocking json-server

1
推荐指数
1
解决办法
8508
查看次数

无法将json-server识别为内部或外部命令

我正在设置一个json服务器并安装它,并将其分配为我的npm start命令,如下所示

"scripts": {
    "start": "json-server -p 3001 -w db.json"
Run Code Online (Sandbox Code Playgroud)

但是每次我在终端上键入npm start时,都会出现此错误

> api-server@1.0.0 start C:\Work\React-projects\streams\api-server
> json-server -p 3001 -w db.json

'json-server' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! api-server@1.0.0 start: `json-server -p 3001 -w db.json`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the api-server@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There …
Run Code Online (Sandbox Code Playgroud)

npm reactjs json-server

1
推荐指数
3
解决办法
3438
查看次数

无法在 JSON-SERVER 中获取对象关键数据

我正在尝试向 json-server 发出 get 请求以获取嵌套对象。但它返回空响应而不是对象关键数据。

我已经阅读了文档,并且没有任何特定于我所拥有的数据库结构的内容。

我想过滤模型数组但不获取模型数据。

我的 db.json 文件是

  {
      "data": {
          "model": 
            [
               {
                  "name": "BERLINGO",
                  "manufacturerName": "CITREON",
                  "id": 1
              },
              {
                  "name": "C3",
                  "manufacturerName": "CITREON",
                  "id": 2
              }
          ]
      }
  }
Run Code Online (Sandbox Code Playgroud)

对于 GET /data ,我收到以下响应。

{
  "model": [
    {
      "name": "BERLINGO",
      "manufacturerName": "CITREON",
      "id": 1
    },
    {
      "name": "C3",
      "manufacturerName": "CITREON",
      "id": 2
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

对 /data/model 的 GET 调用给出了一个空对象。

{}

javascript rest json http json-server

1
推荐指数
1
解决办法
4833
查看次数

在 React 中启动 npm

如何仅通过键入 npm start 来启动这两个功能?

"scripts"{
    "server": "json-server --watch appointmentList.json",
    "start": "react-scripts start",
}
Run Code Online (Sandbox Code Playgroud)

我一直在做的是在不同的终端运行它们

reactjs json-server

1
推荐指数
1
解决办法
303
查看次数

当用作模块时,如何获取json服务器来延迟响应?

json-server 允许通过命令行配置响应延迟:

json-server --port 4000 --delay 1000 db.json
Run Code Online (Sandbox Code Playgroud)

json-server用作模块时,如何尝试做到这一点?以下内容不起作用:

const jsonServer = require('json-server')
var server = jsonServer.create();

server.use(jsonServer.defaults());
server.use(jsonServer.router("db.json"));
server.use(function(req, res, next) {
    setTimeout(next, 1000);
});

server.listen(4000);
Run Code Online (Sandbox Code Playgroud)

它只是setTimeout完全忽略该函数,并且不执行它。

javascript json node.js json-server

0
推荐指数
1
解决办法
1332
查看次数

如何将容器上的端口映射到主机?

我正在尝试运行由以下 Dockerfile 描述的容器:

FROM node:11.4.0

RUN npm install -g sh
RUN npm install -g json-server

WORKDIR /data
VOLUME /data
COPY db.json /data

CMD json-server --watch db.json --port 3001
Run Code Online (Sandbox Code Playgroud)

并通过运行指定侦听端口:

 docker run -it -p 3001:3001 abelalejandro/json-server:final
Run Code Online (Sandbox Code Playgroud)

容器似乎运行良好,json-server 告诉我它正在端口 3001 上处理我的请求,但在浏览http://localhost:3001时我无法获得任何乐趣

在此处输入图片说明

我是否缺少发布/公开端口的内容?

docker dockerfile json-server

0
推荐指数
1
解决办法
240
查看次数

Heroku 错误模块未找到“json-server”

我想在 Heroku 上部署我的服务器,但在 Heroku 构建上它说:

在此输入图像描述

该应用程序在本地运行良好,但不知何故在 Heroku 服务器上崩溃了。

这是文件结构 在此输入图像描述

这是server.js的代码

const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('data.json');
const port = process.env.PORT || 4000;

server.use(router);
server.listen(port);

Run Code Online (Sandbox Code Playgroud)

这是package.json

{
  "name": "server",
  "version": "1.0.0",
  "description": "json-server",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "npm install && node server.js"
  },
  "author": "japsuchiha",
  "license": "ISC",
  "dependencies": {
    "core-util-is": "^1.0.2"
  },
  "devDependencies": {
    "json-server": "^0.15.1"
  }
}
Run Code Online (Sandbox Code Playgroud)

heroku node.js npm server json-server

0
推荐指数
1
解决办法
780
查看次数

测试后反应cypress json-server恢复到原始数据库

嘿,我正在用 cypress 测试 React 应用程序,并使用 json-server 来测试一个假 api。

当我运行一些测试时,它将更改我的 db.json 文件的数据库。

因此计划是在每次测试后将数据库恢复到原始状态。

这是我的代码:

describe('Testing fake db to be restored to original database after 
updating it in other test', () => {

 let originalDb: any = null

    it('Fetch the db.json file and then updating it to the original copy', 
       () => {

      // Save a copy of the original database
      cy.request('GET', 'localhost:8000/db').then(response => {
         originalDb = response;
         console.log(originalDb);
       })

    // Restore the original database
    cy.wrap(originalDb).then(db => {
      cy.request({
        method: 'PUT',
        url: …
Run Code Online (Sandbox Code Playgroud)

testing request reactjs json-server cypress

-2
推荐指数
1
解决办法
184
查看次数