Layer7 API Management

Node.js Backend API for demo purposes 

Jan 23, 2018 02:43 PM

Hello API Team.

 

I've created a bookstore API using Node.js and have been using it in my demos, especially with the Portal. I decided to share it with a broader audience and let anybody out there benefit from it as well.

 

The REST API has two major routes, supporting multiple HTTP verbs to do different things as follows:

Pre-requisites:

 

  • You need to have Node.js installed. If you don't have, just follow this link
  • The API uses MongoDB as its database to store both books and orders information. I'm running the Node.js and MongoDB in the same host and the API is configured to access mongo via localhost and without any authentication. Make sure you have MongoDB installed before you start the NPM. People using Mac can install it via homebrew. Just click here for the instructions.

 

Getting the API:

 

==> This is the git repository where you can find the API <==

==> API Definition - Swagger <==

 

I will be showing you how to get this API running in your Mac. However, you can use whatever you want as long as you have Node.js and MongoDB installed in there.

 

Within your terminal go to the folder where you want to clone the git repository and type clone the repository as follows: 

 

$ git clone https://github.com/alancota/Nodejs-Backend-APIs

 

Go to the newly created folder and run the following command to install all the modules:

 

$ npm install

Make sure the folder node_modules has been created by running a ls command:

 

To start your backend express server just type: npm start and your server will be started using your local server. Notice that the default ports are 8001 for HTTP and 8002 for HTTPS. You can change that by editing the file nodemon.json. If everything is ok you should see this message:

 

That means your server is up and running and you can go ahead and use the APIs. Attached you will find the swagger file of that API. You can use the swagger to import the API into your own portal. Just make sure you change the host and basepath within the file to match YOUR gateway and the URI of the API in your proxy (API Gateway).

 

Attached you will also find a Postman export with all the endpoints ready to use. You just need to change the host to match your server, if you're not using localhost. Following some request samples:

 

Creating a new book (POST):

 

{
"name": "Lord of the Rings",
"price": 12.99
}

 

Updating an existing book (PATCH):

 

[
{
"propName": "price",
"value": 100.00
},
{
"propName: ": "name",
"value": "Batman"
}
]

 

* notice that you can update all the available books fields (name and price) or just one.

 

Placing a new order (POST):

 

{
"productId": "5a615c0e26fae9d83adbea09"
}

 

* you must use a valid product id.

 

Updating an existing order (PATCH):

 

[
{
"propName": "quantity",
"value": "90"
}
]

 

Here is the API published in my demo environment:

 

 

 

 

 

I hope this can help you as much as it has for me.

Statistics
0 Favorited
2 Views
2 Files
0 Shares
1 Downloads
Attachment(s)
zip file
nodejs-backend-v1-swagger.json.zip   2 KB   1 version
Uploaded - May 29, 2019
zip file
Node.js Bookstore Backend API.postman_collection.json.zip   1 KB   1 version
Uploaded - May 29, 2019

Related Entries and Links

No Related Resource entered.