In this section, you will learn how to use Visual Studio Code to run Node.js code locally and then test it using Postman before deploying it to the actual Node.js server in Microsoft Azure App Service:
- Open your Node.js prompt.
- Run npm init, which will create a package.json. The package.json contains the dependency information for running your Node.js code.
- Run npm install express --save and npm install body-parser --save. express and body-parser are the components required for the webhook you just created.
- Edit package.json and add "script": "./server.js". By default server.js will run.
The following code shows the created package.json file:
{
"name": "fortuneCookie",
"description": "",
"version": "0.1.0",
"author": "Henry Lee <henry@henrylee.link>",
"dependencies": {
"body-parser": "^1.9.0",
"express": "^4.0.0",
"jsonpath": "^0.2.0"
},
"script": "./server.js"
}
- Go to Visual Studio Code, click on the server.js file, and press F5. The Node.js HTTP server will be listening on port 8000.
- Click on line 18, request = req;. You will be adding a breakpoint that gets triggered to stop the code from further executing when you post a method using Postman.
The following screenshot shows the breakpoint at line 18:
Line 18 breakpoint
- Go to Postman and create a POST method to http://localhost:8000/fortuneCookie. In the http body, put { "queryResult": { "action": "input.welcome" } }. Here, you are simulating what Dialogflow will be sending to your Node.js webhook.
The following screenshot shows Postman posting to a Node.js server running locally:
Postman posting to a local Node.js server
- Go to Visual Studio Code editor and you will notice that the code execution stopped at line 18, where the breakpoint is. If you hover over the req object, you will be able to see in detail the contents of the req object. Press F5 to let the code execute.
The following screenshot shows the breakpoint at line 18 and the contents of the req object:
Visual Studio with breakpoint
- Go to Postman and you will see the response received from the Node.js server running locally.
The following screenshot shows Postman receiving the response from the Node.js server running locally:
Postman receives the response from the Node.js server running locally