https://github.com/halfzebra/json-server-example
An exapmple of using json-server to mock REST API
https://github.com/halfzebra/json-server-example
Last synced: 4 months ago
JSON representation
An exapmple of using json-server to mock REST API
- Host: GitHub
- URL: https://github.com/halfzebra/json-server-example
- Owner: halfzebra
- Created: 2016-05-28T11:51:58.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-07-14T11:27:59.000Z (almost 10 years ago)
- Last Synced: 2025-01-20T10:14:11.776Z (over 1 year ago)
- Homepage:
- Size: 1.95 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
### Running json-server
This example shows, how to run a local json-server for development purposes.
Edit `db.json` to alter the data, received from the endpoint.
This example does not use a global installation of json-server.
Find more details on the original repository of [json-server][json-server]
#### Usage
Clone the repository and start your own project.
```bash
$ git clone git@github.com:halfzebra/json-server-example.git my-project
$ cd my-project/
$ rm -rf .git
$ git init
```
Install the latest [Node.js](https://nodejs.org/en/), install dependencies and start the server.
```bash
$ npm i # install deps
$ npm start # start the server, using script
```
#### Use in JavaScript
You might use [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) to send a request to the endpoint.
```js
var request = new Request('http://localhost:3004/posts/', {
headers: new Headers({
'Content-Type': 'application/json'
})
});
fetch(request)
.then(function(res) {
return res.json();
})
.then(function(data) {
// Response data
})
.catch(function(err) {
// Error
});
```
[json-server]: