You will be creating a Node.js backend server that will return a list of PodcastData elements. Here are the steps to create a Node.js backend server:
- Open Visual Studio Code for writing the Node.js server.
- Go to File | Open Folder and select the directory where you will be working with the Node.js server codes. The following shows the folder in Visual Studio Code:
Open folder in Visual Studio Code
- Create three files: package.json, server.js, and web.config.
- In package.json, add name:"podcasts", description:"podcast service", version:"0.1.0", author:"Henry Lee <henry@henrylee.link>", dependencies: body-parser": "^1.9.0", "express": "^4.0.0", "jsonpath": "^0.2.0", and
script: "./server.js". The following shows the package.json content:
{
"name": "podcasts",
"description": "podcast service",
"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"
}
- web.config is needed in order to allow the Microsoft Azure App Service to host the Node.js server. The following shows web.config:
<? xml version = "1.0" encoding = "utf-8" ?>
<configuration>
<system.webServer>
<handlers>
<add name="iisnode" path="server.js" verb="*" modules="iisnode" />
</handlers>
<rewrite>
<rules>
<rule name="DynamicContent">
<match url="/*" />
<action type="Rewrite" url="server.js" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
- Create server.js, which is responsible for sending the JSON object of the PodcastDat list. First, declare the express REST server, body-parser for parsing request-and-response objects as JSON, and http for creating the HTTP server. The following shows declaring the server.js global variables:
var express = require('express');
var bodyParser = require('body-parser');
var http = require('http');
var app = express();
var server = http.createServer(app);
app.use(bodyParser.json());
var port = process.env.PORT || 8000;
server.listen(port, function () {
console.log("Server is up and running...");
});
- Create a ping service that simply returns, Welcome to Podcasts Provider. This is very useful for testing whether your Node.js server is deployed successfully and running in Microsoft Azure App Service. The following shows the ping service:
app.use("/ping", function (req, res, next) {
res.send('Welcome to Podcasts Provider');
});
- For the GET podcasts method, you will be creating static PodcastData where the podcast sources and album cover point to the Microsoft Azure blob storage. Also, GET podcasts to check for a security header called mysecret to ensure it will only respond to the request with the correct mysecret value. The following shows the GET podcasts method:
app.get('/podcasts', function (req, res) {
var secret = req.get("mysecret");
if (secret === "12345") {
res.json([
{
id: "1",
podcastSource: "https://henrypodsstorage.blob.core.windows.net/media/HenryPodcast-
1.mp3",
albumName: "Andromeda", artist: "Zepto Segundo", month: "January", title: "Andromeda",
albumCoverSource:
"https://henrypodsstorage.blob.core.windows.net/pictures/zepto_segundo_album_cover.jpg"
},
{
id: "2",
podcastSource: "https://henrypodsstorage.blob.core.windows.net/media/HenryPodcast-
2.mp3",
albumName: "Andromeda", artist: "Zepto Segundo", month: "January", title: "Morir con
Honor",
albumCoverSource:
"https://henrypodsstorage.blob.core.windows.net/pictures/zepto_segundo_album_cover.jpg"
},
{
id: "3",
podcastSource: "https://henrypodsstorage.blob.core.windows.net/media/HenryPodcast-
3.mp3",
albumName: "Andromeda", artist: "Zepto Segundo", month: "March", title: "Ars Chimica",
albumCoverSource:
"https://henrypodsstorage.blob.core.windows.net/pictures/zepto_segundo_album_cover.jpg"
}
]);
}
else {
return res.status(403).end('Access denied!');
}
});
- Hit F5 to run server.js.
- Open Postman to test GET http://localhost:8000/ping and GET http://localhost:8000/podcasts with the 12345 mysecret header. You can refer to Chapter 4, Hosting, Securing, Testing Fortune Cookie in Cloud, if you need a refresher on how to use Postman to debug and test your Node.js server.
- Once tested, refer to Chapter 4, Hosting, Securing, Testing Fortune Cookie in Cloud, for deploying your Node.js server to Microsoft App Service.
- Using Postman, you can test GET https://myhenrytestapp.azurewebsites.net/ping and GET https://myhenrytestapp.azurewebsites.net/podcasts, just like in step 10.