Node Express Send Request and Receive XML Response Tutorial

Last updated on: by Digamber

In this guide, we will show you how to generate XML file in the Node js application. To create the XML file in Node, we will use the XML module as well as the Express package.

XML, also known as Extensible Markup Language, provides a profound way to structure various information.

With XML, You can structure any data such as Data, documents, configuration, books, invoices, transactions, and many more. XML emanated from primitive standard SGML (ISO 8879) to make the web eloquent.

In Node, you can quickly generate the XML. To understand this concept thoroughly, we will install the express and XML packages together. Create a basic API using the express that makes the request to make the users XML in Node.

We have comprised all the topics in smaller steps, go through all the steps to assimilate node XML subject.

How to Make Request with Express and Get XML Response in Node

  • Step 1: Generate New Directory
  • Step 2: Run Npm Init
  • Step 3: Add XML and Express Modules
  • Step 4: Setup Nodemon Server
  • Step 5: Receive XML Response
  • Step 6: Start Node App

Generate New Directory

We will create the new project directory, using the below command.

mkdir node-pro

You can also create the project folder manually.

Right after the folder is generated, enter into the folder using below command.

cd node-pro

Run Npm Init

The most important file of node is the package.json file, you can evoke the file set up by using the initializer command.

npm init

When some questions appear on your terminal screen, make sure to enter the project details.

These details will be added to your package.json file.

Add XML and Express Modules

You are now ready to install the packages that we require to work with xml in node.

By using the following command you will be adding express and xml modules in your node project.

npm install xml express

Setup Nodemon Server

While developing app we go through multiple hit and trials, to see the every change we made we need to start the server over and over again.

To tackle such problem we are using the node package, this will re-start the server on every change.

Go ahead and run the suggested command to add the nodemon package.

npm install nodemon --save-dev

Receive XML Response

Now, you have to create the users.js file at the root of your node project folder.

After you created the file, make sure to register it inside the package.json file.

Here it how your final package.json looks.

{
  "name": "node-pro",
  "version": "1.0.0",
  "description": "",
  "main": "users.js",
  "dependencies": {
    ...
    ...
  },
  "devDependencies": {},
  "scripts": {
    "start": "node users.js"
  },
  "author": "",
  "license": ""
}

Next, open the users.js file and insert the following code into the file.

const express = require('express')
const app = express()
const xml = require('xml')
app.get('/users', (req, res, next) => {
  let xml = `<?xml version="1.0" encoding="UTF-8"?>`
  xml += `<user>`
  for (let i = 0; i < 99; i++) {
    xml += `
    <customer> 
        <firstName>Henry</firstName>
        <lastName>William</lastName>
    </customer>`
  }
  xml += `</user>`
  res.header('Content-Type', 'application/xml')
  res.status(200).send(xml)
})
app.listen(4000, (res) => {
  console.log('Server started on port : 5000')
})

Start Node App

We are now ready to serve the app, and more then ready to see the final output on the browser.

Go to your command line tool, type the command and then execute the given command.

nodemon users.js

Copy the following url type on the browser and press enter.

http://localhost:5000/users

Node Express Send Request and Receive XML Response Tutorial

Conclusion

So, this was it; in this quick tutorial, we helped you learn how to seamlessly create a simple XML file that holds the structured user data.

To form the XML in node, we used the express js and took the XML package’s help.

We combined all the packages; now, you can make the request and create the XML response and see the XML output on the browser with the help of a simple URL.

Digamber

I am Digamber, a full-stack developer and fitness aficionado. I created this site to bestow my coding experience with newbie programmers. I love to write on JavaScript, ECMAScript, React, Angular, Vue, Laravel.