How to Get XML Response From Server in Node with Express

Throughout this quick tutorial, you will learn how to send a request to the server and get the XML response in the Node js application.

To handle the XML response in Node js, we will use th express, XML, and nodemon modules.

XML stands for the extensible markup language; ideally, it is a less complicated text-based representation of structured web information.

At the end of this tutorial, you will be utterly aware of how to send XML request in node js. Also, how to display the XML response in the browser using express.

Node Js Handle XML Response using Express Js Example

  • Step 1: Generate Project Folder
  • Step 2: Set Up Project Package File
  • Step 3: Install Express and XML
  • Step 4: Add Nodemon Library
  • Step 5: Make XML Request and Response
  • Step 6: View XML Response in Browser

Generate Project Folder

First of all, you need to generate the project folder.

We will keep the project files that contain the node project code in this folder.

mkdir node-demo

Now, step into the project folder using given below command.

cd node-demo

Set Up Project Package File

In node, we have to keep the script and project details in one file. It is known by the name of the package.json file.

Move to the command prompt; type the given command in the console, and execute the command to initialize the process.

npm init

After running the above command, some questions will appear on your console screen. You can follow the instructions to add details regarding your node project.

Install Express and XML

In this step, we will go ahead and install express and xml modules simultaneously.

Here are the two commands that you need to enter into the console and then invoke.

npm install express
npm install xml

Add Nodemon Library

We need some kind of module that helps us develop the node js application.

In node, we need to restart the server every time we change something in the code.

In this case, the nodemon module can help us, so go ahead and add nodemon in the node app.

npm install nodemon --save-dev

Make XML Request and Response

In your node project directory, precisely at the root location create a new file name it notes-xml.js.

Afterward open the file and insert the following code into it.

const express = require('express')
const xml = require('xml')
const app = express()

app.get('/notes', (req, res, next) => {
  let xmlData = `<?xml version="1.0" encoding="UTF-8"?>`
  xmlData += `<note>`

  for (let i = 0; i < 55; i++) {
    xmlData += `
    <item> 
        <to>Sam</to>
        <from>Kane</from>
        <heading>Reminder</heading>
        <body>How r ya all!</body>
    </item>`
  }

  xmlData += `</note>`

  res.header('Content-Type', 'application/xml')
  res.status(200).send(xmlData)
})

app.listen(4000, (res) => {
  console.log('Server started on port : 4000')
})

View XML Response in Browser

In the last segment of this guide, we will check how to make request using express api and render the xml response in the browser.

Without further ado, let’s run the node server using the suggested command.

nodemon notes-xml.js

Immediately after open the browser, and paste the given url on the browser.

http://localhost:4000/notes

Here is the final response that appears on the browser window:

How to Get XML Response in Node using Express Js

Summary

When it comes to development in a node environment, nothing is impossible. That’s what we tried to prove in this simple tutorial; so far, we have seen how to precisely make the request to the server.

Fetch the XML data or the XML response in the browser within a node js environment; we took the help of express API and the XML module.

We hope you have carefully followed this concise guide and got what you were really looking for.