Node-Essentials V2 📦
Tags: node, express, npm, rest
Published: 2022-03-04 10:56:23

## Intro 🐱‍💻
So first off, I just want to thank everyone that used my first and only NPM package I've ever created.

I pretty much abandoned the project after getting more experienced these past few years but today I decided I wanted to update this package and make it a little bit more useful.

How the original project came to be:

> 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.

Link to the old version and legacy post: [Node-Essentials](https://dev.to/nicmeister/node-essentials-8pm)

<br/>

## About Node-Essentials 🐱‍🏍
This is a **nodejs** toolkit for doing asynchronous **REST API** calls, writing files to the system, starting up express-js servers and more coming soon.

<br/>

## Setup 🚧
Run:
```cmd
npm install node-essentials
```

And include it in your app:
```javascript
const node = require('node-essentials');
```

or

```javascript
import * as node from 'node-essentials';
```
<br/>

## Current Methods ⚙
* [http](#http) - REST Requests.
    * [get](#get) - Simple REST "GET" request.
    * [post](#post) - Simple REST "POST" request.
    * [put](#put) - Simple REST "PUT" request.

* [helpers](#helpers) - Helper functions.
    * [distinct](#distinct) - Return a Distinct array.

* [server](#server) - Express server.
    * [start](#start) - Set-up a express-js server on the specified port and directory.

* [fileManager](#fileManager) - Manage files.
    * [writeToFile](#writeToFile) - Writes anything passed through to storage as any file.

<br/>

## Quickstart
### <a id="get">Simple Get</a>
Want to quickly and easily retrieve data from an API?
```typescript
executeGet(url: string, options?: any): Promise<any>
 ```
 Implementation:
```javascript
import http from 'node-essentials';

async function getData() {
    const data = await http.executeGet("https://my-json-server.typicode.com/typicode/demo/db").then();
    console.log(data); 
}
```
Response:
```json
{
  posts: [
    { id: 1, title: 'Post 1' },
    { id: 2, title: 'Post 2' },
    { id: 3, title: 'Post 3' }
  ],
  comments: [
    { id: 1, body: 'some comment', postId: 1 },
    { id: 2, body: 'some comment', postId: 1 }
  ],
  profile: { name: 'typicode' }
}
```
<br/>

### <a id="distinct">Distinct</a>
Want to easily return distinct values from an array?
```typescript
distinct(array: Array[any]);
 ```
 Implementation:
```typescript
import helpers from 'node-essentials';

const nonDistinct = [1,1,1,2,3];

function getDistinct(){
    const distinct = helpers.distinct(nonDistinct);
    console.log(distinct);
}
 ```
Response:
```javascript
[1,2,3]
```

<br/>

### <a id="writeToFile">Write To File</a>
Want to write files to storage with data?
```typescript
writeToFile(folder: string, fileName: string, extension: string, data: any);
 ```
  Implementation:
```typescript
import fileManager from 'node-essentials';

const data = {
    sample: "Sample Data"
}

fileManager.writeToFile("./", "fileName", "json", JSON.stringify(data));
```

<br/>

## 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.

<br/>

## Where to get the package

* [NPM](https://www.npmjs.com/package/node-essentials)
* [Github](https://github.com/NicmeisteR/Node-Essentials)

<br/>

## Author

Node Essentials is Developed and Maintained by **[Nicolaas Nel](https://github.com/NicmeisteR)**

[![Facebook](https://img.shields.io/badge/facebook-%231877F2.svg?&style=for-the-badge&logo=facebook&logoColor=white)](https://facebook.com/nicmstr) [![Twitter](https://img.shields.io/badge/twitter-%231DA1F2.svg?&style=for-the-badge&logo=twitter&logoColor=white)](https://twitter.com/NicmeistaR) [![LinkedIn](https://img.shields.io/badge/linkedin-%230077B5.svg?&style=for-the-badge&logo=linkedin&logoColor=white)](https://linkedin.com/in/Nicolaas-Nel) [![YouTube](https://img.shields.io/badge/youtube-%23FF0000.svg?&style=for-the-badge&logo=youtube&logoColor=white)](https://youtube.com/c/NicmeisteR) [![DEV](https://img.shields.io/badge/DEV-%23000000.svg?&style=for-the-badge&logo=dev.to&logoColor=white)](https://dev.to/NicmeisteR)

---

Made with 💙 and ☕ by Nicolaas Nel.