Sample DevOps question - Setting up a Node server and making HTTP calls to validate

The candidate is provided with a Ubuntu 20.04 machine where the candidate can install the Node.js server and validate HTTP call responses.

To learn more about how to create a custom DevOps question, read this article.

Use case

Setting up Node Web Server and validating HTTP calls. The candidate is provided with an Ubuntu Virtual Machine (VM). The initial configuration is being done using the configuration script. The candidate needs to run commands to set up and deploy a Node.js server such that the server is able to respond to the HTTP requests. The candidate's evaluation will be done with respect to the test cases mentioned in the validation script. 

Sample problem

Problem name: Node.js Web Server

Tasks

  1. Install 'Node.js'.
  2. Run a Node HTTP server on port '8001'.
  3. GET request on /test should return the text 'Hello From Node'.
  4. Any other request must respond with the text '404-Not Found' for any other routes.
  5. Keep the Node Server running and Execute.

Validation script (Test cases)

output0=$(curl -s http://127.0.0.1:8001/test)

found="Hello From Node"

notFound="404-Not Found"

outputnode=$(which node)




if [[ -n "$outputnode" ]]; then

echo "TESTRESULT nodetest 10"

else

echo "TESTRESULT nodetest 0"

fi

# GET /test

if [ "$output0" == "$found" ]; then

echo "TESTRESULT GETtest 10"

else

echo "TESTRESULT GETtest 0"

fi

output1=$(curl -s -X POST http://127.0.0.1:8001/test)

# POST /test

if [ "$output1" == "$notFound" ]; then

echo "TESTRESULT POSTtest 10"

else

echo "TESTRESULT POSTtest 0"

fi

output2=$(curl -s http://127.0.0.1:8001/)

# GET /

if [ "$output2" == "$notFound" ]; then

echo "TESTRESULT GETtest2 10"

else

echo "TESTRESULT GETtest2 0"

fi

# POST /

output3=$(curl -s -X POST http://127.0.0.1:8001/)

if [ "$output3" == "$notFound" ]; then

echo "TESTRESULT POSTtest2 10"

else

echo "TESTRESULT POSTtest2 0"

fi

Explanation of test cases

  1. The script makes a GET request to http://127.0.0.1:8001/test and stores the response in the output0 variable.
  2. The script defines two variables: found with the value "Hello From Node" and notFound with the value "404-Not Found".
  3. It checks if Node.js is installed by finding the location of the node executable and stores it in the outputnode variable. If outputnode is not empty, it provides the output "TESTRESULT nodetest 10"; otherwise, it provides the output "TESTRESULT nodetest 0".
  4. It compares output0 with found to check if the GET request response matches the expected "Hello From Node" message. If the message matches, the script provides the output "TESTRESULT GETtest 10"; otherwise, it provides the output "TESTRESULT GETtest 0".
  5. It makes a POST request to http://127.0.0.1:8001/test and stores the response in the output1 variable.
  6. It compares output1 with notFound to check if the POST request response matches the expected "404-Not Found" message. If the message matches, the script provides the output "TESTRESULT POSTtest 10"; otherwise, it provides the output "TESTRESULT POSTtest 0".
  7. It makes a GET request to http://127.0.0.1:8001/ and stores the response in the output2 variable.
  8. It compares output2 with notFound to check if the GET request to the root endpoint response matches the expected "404-Not Found" message. If the message matches, it provides the output "TESTRESULT GETtest2 10"; otherwise, it provides the output "TESTRESULT GETtest2 0".
  9. It makes a POST request to http://127.0.0.1:8001/ and stores the response in the output3 variable.
  10. It compares output3 with notFound to check if the POST request to the root endpoint response matches the expected "404-Not Found" message. If they match, it outputs "TESTRESULT POSTtest2 10"; otherwise, it outputs "TESTRESULT POSTtest2 0".

Configuration script

The command 

mkdir/home/ubuntu/code/NodeWebServer

creates a new directory named "NodeWebServer" within the 

/home/ubuntu/code 

directory path. 

Solution

# install node,npm

$ sudo apt-get update
$ sudo apt-get install nodejs -y
$ sudo apt-get install npm -y

# creating a node project
$ mkdir solution
$ cd solution
$ npm i

# installing http package

$ npm i http

# creating index.js with following content

$ vim index.js

const http = require("http");
// PORT
const PORT = 8001;
// server create
const server = http.createServer((req, res) => {
if (req.url === "/test" && req.method === "GET") {
res.write("Hello From Node");
res.end();
}else {
res.write("404-Not Found");
res.end();
}
});
// server listen port
server.listen(PORT);
console.log(`Server is running on PORT: ${PORT}`);

# run the server

$ node index.js