I am working on a project that integrates with multiple other vendors using Http endpoint. We send http requests with JSON payload and the vendors has to consume that and respond with a response with JSON payload. I wanted to have a simple dummy site that takes the request and responds with the same kind of data I expect from the vendors. The whole objective of having this is to mock the vendor and test the integration. Moreover, I wanted an easy and fast process to create, deploy and start multiple mock vendor sites.
Node.js can be used to achieve this. For those of you who are already familiar with javascript, the code needed to create and run these sites is very minimal.
Note: this post is not supposed to be a tutorial on Node.js. That would be a topic for another day.Once I am comfortable enough with the nitty gritty details of Node.js, I hope to come back with a post.
I am using a Windows machine and here are the steps I followed,
1. Install Node.js from here
- Node.js will install a console window that you can use as a playground or IDE. Here you can write and run any JavaScript code
- Node.js root directory is included in the PATH variable of system environment variables . Thus you can invoke the node command from your command prompt
2. I created a folder c:\projects\nodejsfiles
where I keep all my Node.js related files.
Note: You can keep your node file at any directory you want.
Note: You can keep your node file at any directory you want.
3. Copy the code shown below(after step 5) into
notepad and save it as myhttpendpoint.js under the directory I created above.
4. Open Command Prompt and navigate to the path
where the myhttpendpoint.js is located.
5. In my case c:\projects\nodejsfiles and
then run the command
node myhttpendpoint.js
node myhttpendpoint.js
The code looks like the following,
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/json'});
var
responseObj = {
id:
null,
detail: [
{
part1: [
{
property1: 55,
property2: "hello there!",
property3: 12.0,
property4: null,
property5: 0,
property6: true
}
],
}
],
};
res.end(JSON.stringify(responseObj));
}).listen(1330, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1330/');