I needed to build a mock back-end web service this week because the middle-ware team wasn't ready (they're useless standard Java 'enterprise' developers) and @Jason challenged me to write it in node.js. He showed me an example of how this was done before and said it shouldn't take more than 5 minutes to have something up and running, I've done this kind of requirement previously in Web API and thought it would be interesting to finally do 'something' in node.
All coding contexts have been changed to protect the innocents :)
The requirement was for a web service returning JSON defined resources and the idea was to map the request URL to the local the file system, where the actual content to be returned is contained in a *.json file and the actual file-name represent the HTTP code to be returned, some examples are show below:
All coding contexts have been changed to protect the innocents :)
The requirement was for a web service returning JSON defined resources and the idea was to map the request URL to the local the file system, where the actual content to be returned is contained in a *.json file and the actual file-name represent the HTTP code to be returned, some examples are show below:
'http://localhost:1008/examples/user/1' maps to 'd:\work\node\resources\user\1\200.json'
'http://localhost:1008/examples/user/2' maps to 'd:\work\node\resources\user\2\404.json'
'http://localhost:1008/examples/user/3' maps to 'd:\work\node\resources\user\3\302.json'
First, download and install node from here, I went for the standard install using the MSI installer:
Second, download Web Storm evaluation version from here, I decided to try out Web Storm and esque my usual choice of Visual Studio:
After going with the standard install node needs to be configured in Web Storm. This can be done by going to File -> Default Settings and selecting 'node' - and installed the pre-requisties and configure the executable path for the node executable:
Once configured ready to do some node development:)
I created an emtpy project and add a 'server.js' file:
Next was to configure debugging to use node, this is simple, just selected the node configuration and add the 'server.js' to application server path:
The project is ready to go, okay it does nothing, but I can at least start the debugger and see the output in the Web Storm console window:
I used express.js to handle the HTTP requests & responses and node-fs to access the local file system. These are 'added' using node package manager - this is similar to nuGet, except that it is more mature and reliable IMO. These and other modules are referenced in the 'server.js' file as 'require' statements, you can see this in the following screenshot:
The service is going to map HTTP requests to file system locations, the project viewer in Web Storm give a good idea of what I mean, the screenshot below shows the file structure of not only the javascript files but also the mock resources I'm going to use:
So the above example User '1' maps onto a valid response (200 HTTP code) and returns a User JSON object contained in the 200.json file, user '2' maps onto a unknown resource (404 HTTP code) and will return the message contained in the 404.json file and finally user '3' cause a URL redirect to another User resource as defined in the 302 file.
This could very easily be done using Web API or node.js, either implementations allow me to dynamically add resources as I require - I can modify the types & number of the resources without having to recompile & restart anything for the new resources to work. Having built mock service in Web API before I was interested to find out how much & how quick it would be to build it with node.
First, download and install node from here, I went for the standard install using the MSI installer:
Second, download Web Storm evaluation version from here, I decided to try out Web Storm and esque my usual choice of Visual Studio:
After going with the standard install node needs to be configured in Web Storm. This can be done by going to File -> Default Settings and selecting 'node' - and installed the pre-requisties and configure the executable path for the node executable:
Once configured ready to do some node development:)
I created an emtpy project and add a 'server.js' file:
Next was to configure debugging to use node, this is simple, just selected the node configuration and add the 'server.js' to application server path:
The project is ready to go, okay it does nothing, but I can at least start the debugger and see the output in the Web Storm console window:
I used express.js to handle the HTTP requests & responses and node-fs to access the local file system. These are 'added' using node package manager - this is similar to nuGet, except that it is more mature and reliable IMO. These and other modules are referenced in the 'server.js' file as 'require' statements, you can see this in the following screenshot:
Loading ....
Hopefully what should be obvious from the above, there are two handlers for HTTP requests, one is for resources under the 'examples' sub path and the other is a wildcard for everything else which will return a 404 HTTP code. The other thing to note is the use of the options module, this is for parsing command line parameters and this give the ability to specify a port for HTTP server (defaults to port 1008 if not specified).The service is going to map HTTP requests to file system locations, the project viewer in Web Storm give a good idea of what I mean, the screenshot below shows the file structure of not only the javascript files but also the mock resources I'm going to use:
The resource is going to be a 'User', and as you can see there will be three available, id = 7, id =8 & id = 9. Each will return a different HTTP code and possible data as described:
Id = 7 - this will return HTTP code 200, with the contents (json) of the 200.json file,
Id = 8 - this will return HTTP code 404, with the content (message) of the 404.json file,
Id = 9 - this will return HTTP code 302, with the header (location) of the 302.json file,
So this pattern allows me to mock out any response scenario from the service - any HTTP code can be mocked.
The other thing to note about the above screenshot is the 'statusCodes.js' file - this is a locally defined npm module. It's responiblity is to dynamically load the other local modules in the 'status_code_handlers' folder, these modules provide the specific logic for full-filling a HTTP code, e.g. the '200.js' file is shown below:
The completed 'server.js' file looks like this:
Requesting User resource Id = 7 return a 200 code with the specific data (json):
Requesting User resource for Id = 8 returns a 404 with the specific error message:
Requesting User resource for Id = 9 redirects to User Resource Id = 7, hence the two requests seen in Fiddler:
That pretty much covers it, it's been a much quicker refactoring loop using Web Storm and node instead of Visual Studio and Web API - Web Storm is a much more responsive development environment, quicker to load, quicker to start a debugger etc...
The code is available for download:
Id = 7 - this will return HTTP code 200, with the contents (json) of the 200.json file,
Id = 8 - this will return HTTP code 404, with the content (message) of the 404.json file,
Id = 9 - this will return HTTP code 302, with the header (location) of the 302.json file,
So this pattern allows me to mock out any response scenario from the service - any HTTP code can be mocked.
The other thing to note about the above screenshot is the 'statusCodes.js' file - this is a locally defined npm module. It's responiblity is to dynamically load the other local modules in the 'status_code_handlers' folder, these modules provide the specific logic for full-filling a HTTP code, e.g. the '200.js' file is shown below:
The completed 'server.js' file looks like this:
Loading ....
Running the server up I get the following responses in Fiddler:Requesting User resource Id = 7 return a 200 code with the specific data (json):
Requesting User resource for Id = 8 returns a 404 with the specific error message:
Requesting User resource for Id = 9 redirects to User Resource Id = 7, hence the two requests seen in Fiddler:
That pretty much covers it, it's been a much quicker refactoring loop using Web Storm and node instead of Visual Studio and Web API - Web Storm is a much more responsive development environment, quicker to load, quicker to start a debugger etc...
The code is available for download:
Comments
Post a Comment