Skip to main content

2 posts tagged with "npm"

View All Tags

Node-Essentials

Nicolaas Nel

Nicolaas Nel

Lead Front End Product Developer @ InfoSlips

Late last night I decided to finally publish my very own npm package, it's something I've wanted to do for a while now and I could never decide what to package.

While brainstorming I was busy working on a node.js app to process some data, the same code I've used in several projects.

This was it, the perfect package. I don't like searching for stuff I've already used and will use again in the future so I decided to package my most used node functions into a lightweight npm package.

To my surprise in under 10 hours it already hit over 800 downloads!๐Ÿฅณ

About Node-Essentials ๐Ÿฑโ€๐Ÿ#

This is a nodejs toolkit for doing asynchronous api calls, writing files to the system, starting up express-js servers and more coming soon.

Setup ๐Ÿšง#

Run:

npm install node-essentials

And include it in your app:

let node = require('node-essentials');

Current Tools โš™#

writeToFile - Writes anything passed through to storage as any file.#

The below sample is used to print out json data from an API call.

node.writeToFile("tests", "writeToFile", "json", JSON.stringify(data));

express - Set up an express-js server on the specified port and directory.#

The "__dirname" is the relative path for the directory used by node and 3000 is the port it will serve to this is also the default express port. This would serve the current root directory.

node.express(3000, __dirname);

get - A simple Async REST "get" request.#

I will be adding more rest functions soon.

async function get() {
try {
test = await node.get(
"https://apiurlhere.com/",["api key desc", "api key value"])
.then(console.log("done"));
}
finally {
console.log(test);
}
};

Conclusion ๐Ÿ#

The package contains functions I commonly use, I will be adding more simplified essential tools/functions as time goes on!

It's been an interesting journey making my first package and any feedback/suggestions of functions to add would be welcomed.

Where to get the package#

NPM Github

If you want to keep track of all my projects and endeavors I am super active over on Twitter and I'm always happy to give advice and lend some help.

FinalNecessity on Twitter ๐Ÿฃ

GraphQL - NodeJS

Nicolaas Nel

Nicolaas Nel

Lead Front End Product Developer @ InfoSlips

For a while now I've wanted to jump into the deep end of GraphQL and see what all the fuzz is about.

And oh my I wish I did it sooner.

So, what is it?#

GraphQL is A query language for your API, this gives you the ability to ask for exactly what you need from the API and nothing more.

Why should I use it?#

This is something that works amazing for big API's like the Halo 5 Game API that I used in my example.

A normal player has about 20 000 lines of data but for most of the calls I make I only use a fraction of that (24 to be exact). Reducing my calls to only get what I need gives me a big performance bonus and I'm able to use less data on the client side.

Returning a smaller JSON object makes coding on the front end a lot easier than previously.

// Sample of Possible Queries
let queryObjectSample = {
Gamertag: String,
Xp: Number,
SpartanRank: Number,
HighestCsrAttained: {
Tier: Number,
DesignationId: Number,
Csr: Number,
PercentToNextTier: Number,
Rank: Number
},
Stats: {
TotalKills: Number,
TotalHeadshots: Number,
TotalMeleeKills: Number,
TotalAssassinations: Number,
TotalGroundPoundKills: Number,
TotalShoulderBashKills: Number,
TotalPowerWeaponKills: Number,
TotalDeaths: Number,
TotalAssists: Number,
TotalGamesCompleted: Number,
TotalGamesWon: Number,
TotalGamesLost: Number,
TotalGamesTied: Number,
TotalGrenadeKills: Number,
TotalSpartanKills: Number,
},
TotalTimePlayed: String
}

Here is a sample of what I return opposed to the 20 000 fields:

{
"data": {
"Gamertag": "NicmeisteR",
"HighestCsrAttained": {
"Csr": 1872,
"Rank": 78
},
"Stats": {
"TotalKills": 34834,
"TotalDeaths": 31017
}
}
}

To get the above data all I have to do is make an API call to my Express NodeJS server and with the below query to retrieve what I need.

var dataString = {
"query":
`{
Gamertag,
HighestCsrAttained {Csr, Rank},
Stats { TotalKills, TotalDeaths}
}
`};

How to get started#

Go over to GraphQL and select your language of choice and follow through with the tutorial.

GraphQL is super scalable and flexible.

Alternatively you can also check out my Github Repository GraphQL-NodeJS to get straight into the action and see how it all comes together.

Conclusion#

GraphQL is something I would high recommend checking out, although it's still relatively new a lot of employers are on the lookout for people with knowledge around it.

Check out the repository for the project here: https://github.com/NicmeisteR/GraphQL-NodeJS