Skip to main content

Parsing a Request in Node JS

Learn how to parse and use a Request in Node JS.

Requirements

The Requirements for this Tutorial are:

Setting up

To Start, we will Setup our Dev Environment. You can skip this Step if you are already setup with a Terminal and your Editor
Startup your Terminal and Type:

npm init -y
danger

This will Setup a Node JS Project in the current folder. If you do not want that, then you will first have to change your Directory.

The Request

Then you will need to create a new file called "index.js". Open it, and create a new Fetch Request. After that your Code should work like this:

var requestOptions = {
method: 'GET',
redirect: 'follow'
};

fetch("https://api.izanami.dev/destiny/v1/weapon/235827225", requestOptions)
.then(response => response.text())
.then(result => {
console.log(result)
})
.catch(error => console.log('error', error));

The console.log(result) will return the Response from the API to your Terminal.
Run it with:

node index.js

The output should look like this:

{
"statusName": "Ok",
"isError": false,
"statusCode": 200,
"msg": {
"hash": 235827225,
"collectibleHash": 1042746134,
"name": "Eyasluna",
"classType": 3,
"classTypeName": "Unknown",
"itemType": 3,
"itemTypeName": "Weapon",
"damageType": 151347233,
"damageTypeName": "Stasis",
"ammoType": 1,
"ammoTypeName": "Primary",
"equipmentSlot": 1498876634,
"equipmentSlotName": "Kinetic Weapons",
"breakerType": 0,
"breakerTypeName": "None",
"specialItemType": 0,
"specialItemTypeName": "null",
"powerCap": 999990,
"sourceString": "Source: 'Grasp of Avarice' Dungeon",
"collectibleIcon": "/common/destiny2_content/icons/6a71ffaef39e6bad084b933b3f034ce6.jpg",
"itemSubTypeName": "Hand Cannon",
"itemSubType": 9,
"tierType": "Legendary",
"isRandomlyRolled": 1,
"icon": "235827225/collectible/icon",
"perks": [
{
"hash": 1294026524,
"perkName": "Adaptive Frame",
"column": 1,
"description": "A well-rounded grip, reliable and sturdy.",
"perkExplanation": "501 - Not Implemented",
"statArea": []
}
//output Ommitted

],
"stats": [
{
"statName": "Stability",
"statValue": 64,
"statHash": 155624089,
"statExplanation": "501 - Not Implemented"
},
//output Ommited
]
}
}
caution

Your Output will be a lot longer, it is shortened here for performance Reasons.

Getting the Data out of the Object.

To get the Data from the Object that we queried from the API, we will use something like this.

var requestOptions = {
method: 'GET',
redirect: 'follow'
};

fetch("https://api.izanami.dev/destiny/v1/weapon/235827225", requestOptions)
.then(response => response.text())
.then(result => {
let jsonResult = JSON.parse(result)
console.log(jsonResult.msg.name)
})
.catch(error => console.log('error', error));

Then run it with:

node index.js

OUTPUT

Eyasluna

What we did here:

  • We added a variable called jsonResult and assigned the Value of JSON.parse(result) which, is a function and returns an Object for us to use.
  • Then we used console.log(jsonResult.msg.name) to print out the Name of the Gun - Eyasluna - to the Terminal
  • in this case the .msg corresponds to one of the keys in the JSON object that we received earlier. With `.msg.name`` we can get a value from an Object that is in an Object and that is exactly what we did here.