References
Setting up a fake JSON REST API
|
|
In the server folder, create a database.json
file and add the following JSON object:
|
|
This JSON file will act as a database for your REST API server. You can simply add some data to be served by your REST API or use Faker.js
for automatically generating massive amounts of realistic fake data.
|
|
Now, create a generate.js
file inside server
directory and add the following code:
|
|
We converted the database object to a string and log it to standard output.
Next, add the generate and server scripts to the package.json
file:
|
|
Run the generate script using the following command:
|
|
Finally, run the REST API server by executing the following command:
|
|
Your server will be available from the http://localhost:3000/ address.
GET
/products
for getting the products,GET
/products/<id>
for getting a single product by id,POST
/products
for creating a new product,PUT
/products/<id>
for updating a product by id,PATCH
/products/<id>
for partially updating a product by id,DELETE
/products/<id>
for deleting a product by id.
You can use _page
and _limit
parameters to get paginated data. In the Link header you’ll get first
, prev
, next
and last
links.
GET
/products?_page=1
for getting the first page of dataGET
/products?_page=1&_limit=5
for getting the first five products of the first page of data.
For the features such as filters, sorting and ordering, check out the docs.