json-server | full fake REST API | REST library
kandi X-RAY | json-server Summary
kandi X-RAY | json-server Summary
Get a full fake REST API with zero coding in less than 30 seconds (seriously). Created with <3 for front-end developers who need a quick back-end for prototyping and mocking.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- list resource names
- Start the server
- Creates a new app .
- Update a resource .
- Get a list of references from the collection .
- get page object
- Display a resource
- Prints the list of resources
- Remove document related to the database
- Block to produce custom routes
json-server Key Features
json-server Examples and Code Snippets
var express = require('express');
var jsonServer = require('json-server');
var app = express();
//publish the static result of npm run build (vue)
//https://github.com/jrichardsz/nodejs-express-snippets/tree/master/simple-static-server
a
# Stage 1
FROM node:10-alpine as build-step
RUN mkdir -p /app
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
RUN npm run build --prod
# Stage 2
FROM ubuntu:16.04
USER root
WORKDIR /home/app
COPY package.json /home/app/pac
FROM node:10-alpine as build-step
RUN mkdir -p /app
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
RUN npm run build --prod
# Stage 2
FROM nginx:1.17.1-alpine
COPY nginx.conf /etc/nginx/nginx.conf
npm install -g json-server
{
"posts": [
{ "id": 1, "title": "json-server", "author": "typicode" }
]
}
json-server db.json
json-server db.json -p 3001
version: '3'
services:
json-server:
image: json-server-image:latest
ports:
- "3000:3000"
container_name: json-server
hostname: json-server
networks:
- some-net
angular:
image: angular-image:latest
npm install -g json-server
npx json-server --watch db.json
npm i -D @angular-builders/custom-webpack json-server
ng config projects..architect.build.builder @angular-builders/custom-webpack:browser
ng config projects..architect.build.options.customWebpackConfig.path custom-webpack.config.js
ng con
npm install -g json-server
echo '{"logs": []}'> logs.json
json-server logs.json
let veryLargeTextWorks = "1234567890";
for (let i = 0; i < 11; i++) {
FROM node:lts-slim
RUN npm install -g json-server
WORKDIR /data
VOLUME /data
EXPOSE 3000
ADD entrypoint.sh /entrypoint.sh
ENTRYPOINT ["bash", "/entrypoint.sh"]
CMD []
#!/bin/bash
args="$@"
args="$@ --host 0.0.
// api-routes.js
const express = require('express')
const jsonServer = require('json-server')
const router = express.Router()
const server = jsonServer.create()
const middlewares = jsonServer.defaults()
const v1Router = jsonServer.router(
Community Discussions
Trending Discussions on json-server
QUESTION
My setter, for SetPersons, seems to keep adding empty elements into its array for no apparent reason. I am trying to make my phone number list update instantaneously when I update a user's number but it sticks with the old number. Whenever I apply the logic to possibly make the number update instantly, it adds empty elements into my array instead and does not update instantly. Here is the code for each separate component:
App.js
...ANSWER
Answered 2022-Apr-18 at 01:06There are issues with the logic where you update persons
.
Try this instead:
QUESTION
I'm using json-server as my local server for a nuxt project and i want to automatically launch the server and then run the project on another shell instance using "npm run dev"
in the scripts tag in package.json this is what i came up with :
...ANSWER
Answered 2022-Mar-23 at 11:29QUESTION
Learning reactjs, trying to simulate a server with json-server
this is my script to run the server:
ANSWER
Answered 2021-Dec-18 at 13:15the problem was with the port. changed it to 3001 and it works. still doesn't understand why did this happen in that port.
QUESTION
Image of the error This is the error that I am continuously getting after numerous tries. I tried removing node cache, I re-installed node.js but couldn't find success. However, whenever I am running it with administrator or root, I am able to install it. So why is this happening?
I have provided the screenshot of my error, however, still, I will provide the error in writing.
...ANSWER
Answered 2021-Sep-03 at 07:58Try running this command:
npm config set unsafe-perm=true
If that doesn't work for you, try changing the folder permissions for your current user for C:\Program Files\nodejs
with the help of this
QUESTION
Attempting to make a HTTP GET
request for posts filtered by query parameter authorId=x
where x
may be a number that may not correspond to any post's authorId
.
json-server
unexpectedly returns HTTP 200
instead of HTTP 404
response when there are no posts with a matching authorId
(i.e., an empty array is returned), how can this be changed to return 404
? Similarly, what would be the best API practice here, would it be to return an empty array with HTTP 400
as json-server
already does or would returning the empty array with HTTP 404
be more clear for users?
I've looked at jsonServer.rewriter
& express
middleware (e.g., json-server
documentation shows it can be configured with middleware such as server.use(middlewares)
) but wanted to ask what the best approach would be here (useful resources/links would be appreciated here), e.g., with middleware, an option is sending 404
for an empty array but does json-server
have an builtin ways to handle this or is there a better approach?
All constructive feedback is welcome, thanks.
Codedb.json
:
ANSWER
Answered 2022-Feb-24 at 18:42As @kindall pointed out, probably ill-advised to return 404, but perhaps this is the existing behavior of the API you're mocking. You can return custom output - from docs: https://github.com/typicode/json-server#custom-output-example
QUESTION
I'm pretty sure doing everything correctly. I'm using these version:
...ANSWER
Answered 2021-Oct-30 at 03:31According to @user2740650
You said db.json is in src folder. What matters is that it's in the same folder where you started the server. It sounds like it created a default db.json somewhere else and is using that.
Second Scenario
move your db.json file into the Public folder and calling it by: axios.get('db.json') .then(//...)
QUESTION
I am trying to install JSON-Server, but it's not working!
I have used the following command: npm install -g json-server
Here is the error message:
...ANSWER
Answered 2021-Nov-23 at 13:06Try to use sudo before command :
sudo npm install -g json-server
QUESTION
I have installed the json-server
in my Angular app which enables me to save a form (filled by the user) via a fake json API into a json file:
ANSWER
Answered 2021-Oct-13 at 09:21Please try this solution. It is using the same blob just with some constant data.
in html
QUESTION
what is -w
in here means ?
ANSWER
Answered 2021-Nov-16 at 09:23--watch, -w Watch file(s) [boolean]
QUESTION
As I develop the backend side, I also develop the frontend. As you have probably guessed, I have to do some REST requests on my server.
Well, there are a couple of solutions for that. I can use mocks on the frontend, or a fake JSON server. However, there are some cons to this approach:
- I have to remodel what I have on an already existing backend server.
- Dealing with authentication on a fake JSON server is not really comfortable.
- I have to write a lot of boilerplate if I want to go on mocking and unit testing approach.
Again, all the logic I want already exists in my Django project, so what I want is:
- Run a dev server.
- Set up a fresh new database.
- Provide some initial data. Some fake users, posts, whatever.
- Do my testing on my frontend project while the server runs.
- Hit the kill signal. (CTRL+C)
- Gracefully drop the database that I have created on step 2.
- Shutdown the program.
I use pytest
and pytest-django
. They do exactly these when they set up a test environment, I was wondering if I can manually do that.
Thanks in advance.
Environment- Python 3.9
- Django 2.2
ANSWER
Answered 2021-Nov-16 at 11:12I never used such integration-testing setup, but I’ve seen it somewhere.
You can run the django-admin testserver mydata.json
command to spin up a testing server with data from a provided fixture. The way it was done in the example I saw was by making a custom Django command where instead of static fixture the testserver’s database was populated dynamically by factories. I don’t remember the details how it was implemented, though.
After the testserver was ready, the Cypress tests were run. Backend and the frontend were wired by npm’s start-server-and-test
package.
Application with similar setup can be checked out here. Especially note the custom testserver command as well as the cypress commands.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install json-server
If you make POST, PUT, PATCH or DELETE requests, changes will be automatically and safely saved to db.json using lowdb.
Your request body JSON should be object enclosed, just like the GET output. (for example {"name": "Foobar"})
Id values are not mutable. Any id value in the body of your PUT or PATCH request will be ignored. Only a value set in a POST request will be respected, but only if not already taken.
A POST, PUT or PATCH request should include a Content-Type: application/json header to use the JSON in the request body. Otherwise it will return a 2XX status code, but without changes being made to the data.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page