Tutorial 1: Simple REST API Endpoints
One of the first exercises taught to those interested in making a server is usually to create an echo server. Our echo server will use restify to capture a parameter passed through the URL, and return that parameter with a simple messages. Create a file named echo-server.js and copy the following code:
Using restify, we create a single endpoint '/echo/:name', where :name is a parameter passed through the URI. To run the server, simply open a terminal in your project's directory and run:
node echo-server.js
You should see something similar to restify listening at http://[::]:8080
as a response. Simply navigate to your server in your browser, with a URL similar to <yourserver>:8080/echo/george. You should see returned in your browser "Hello, george".
Of course, APIs can be accessed by more than just a browser. Create another file called echo-client.js and copy the following code:
Note, we are requiring another package to run our client called 'restify-clients'. To install it in your project, simply run
npm install restify-clients --save
Make sure your echoserver is still running. In a new terminal, navigate to the same directory and run
node echo-client.js
You should see a message in the terminal of "Server returned: "Hello, mark"". Our client worked! Notice that because our client is on the same machine as the server, we can just call localhost:8080 instead of the URL to our IBM i. Clients are a great way for your front end code to retrieve data from your back-end server, thereby programatically using your API!
Last updated