In this blog, we’ll go even further with the types of responses we want to call from a web service. As you’ll recall from the previous blog posts, we started by creating a new space in NodeRun, and selecting the “REST Web Services” template. You can view the Space I am working with in this example.
Create a Service to Get a Single Employee
In the previous blog, I talked about creating web services that enabled you to filter results by Employee Name, and also to limit the number of results per page.
Now let’s create a service to call up a single Employee record. To do this, I created a new file in the Employees folder called ‘getEmployee.js’:
Then I went to the properties of ‘getEmployee.js’ and set up the service path:
- Checked the Express Route box
- Set the HTTP Method to GET
- Set the Route Path to employees/:employeeNumber
- Important to note: the ‘:’ in the route path. This colon tells Express.js that employeeNumber will be a parameter and it should pass the value onto the program within the request.params object.
- A more advanced example:
- Get the time sheet details for week 3, for employee 5.
In the Advanced REST Client (ARC) Chrome add on, I changed the Request URL to end with ‘/employees/1002’, and it returned a JSON object with the details of that employee.
I changed the Request URL to an invalid employee number, /employee/ABC, and it returned a null record.
For more information about Node’s Express.js routing works, check out their website.
Create a Service to Add an Employee
Now, let’s add an employee with a web service. I created a new file in that same Employees folder called ‘addEmployee.js’.
I went to the properties of this getEmployee.js file and set up the service path like so:
- Checked on the Express Route
- Set the HTTP Method to PUT
- Set the Route Path to employees (just like ‘getEmployes.js’)
In the Method field in ARC, I choose PUT. In the Request URL field, I changed it to end with /employees.
Next, we need to send more information with the request. We do this by putting this JSON data into ‘body’:
After hitting the send button, it returned an object showing that it there was only one affected row. And just to be sure it added it correctly, I changed the Method to GET and Request URL to ‘/employees/555’, and it returned the details of the ‘George Washington’ record.
Create a Service to Update an Employee
To update an employee record, I created a new file in that same Employees folder called ‘updateEmployee.js’.
I then went to the properties of this file and set up the service path. I:
- Checked on the Express Route
- Set the HTTP Method to POST
- Set the Route Path to employees/:employeeNumber (just like ‘getEmployee.js’)
In ARC, I chose POST in the Method field and I changed the Request URL field to end with ‘/employees/555’.
Next, we want to change some of George Washington’s data. We do this once again by putting JSON data into ‘body’. We will just change the officeCode to “2” and extension to null. Because of how I wrote the service, I do not have to send over any of the unchanged fields.
I hit the Send button and it returned an object showing that there was only 1 affected row. Just to be sure it updated correctly I changed the Method to GET and Request URL to /employees/555 – and it did show the updated values for George Washington.
Create a Service to Delete an Employee
Finally, we can create a service to delete an employee. Here, I created a new file in that same Employees folder called ‘deleteEmployee.js’, with an additional try/catch logic.
Then I went to the properties of this deleteEmployee.js file and set up the service path.
- Check the Express Route box
- Set the HTTP Method to DELETE
- Set the Route Path to employees/:employeeNumber (just like ‘getEmployee.js’ and ‘updateEmployee.js’)
In ARC, I chose DELETE in the Method field and changed the Request URL field to end with ‘/employees/555’.
I hit the Send button and it returned an object showing that it there was only one affected row. Just to be sure it updated it correctly, I changed the Method to GET and Request URL to ‘/employees/555’. The resulting record was ‘null’, indicating there is no employee 555 and that employee was successfully deleted.
I hope you have enjoyed this blog series on creating RESTful web services. These blogs only scratch the surface of what can be accomplished by using web services. The examples I used only do simple database operations against a single table. Imagine creating a process that involves several database operations that are wrapped up into one single RESTful web service. Or even a simple web service that builds a table, combo box list, or just validates data.
In closing, we now have all our simple CRUD web services over the employees table, and it only took a couple of minutes to build on NodeRun. I am sure you see more bells and whistles you’d like to add such as:
- Consistent returned object
- Add in your own utilities, such as a generic function that would extract relevant pieces of the request object, which would make this code even less cluttered and more module.
If you do try these options out on NodeRun, please share your Spaces in the comments section of this blog!