Skip to content

Instantly share code, notes, and snippets.

@NyashaNziramasanga
Last active June 25, 2022 02:16
Show Gist options
  • Save NyashaNziramasanga/e7970aa3576a1932c9d558a164c798d7 to your computer and use it in GitHub Desktop.
Save NyashaNziramasanga/e7970aa3576a1932c9d558a164c798d7 to your computer and use it in GitHub Desktop.
Use NPM Packages

Usefull Node Packages

List of usefull node packages

A Node Web framework

Usage:

npm i express --save

const express = require('express')
const app = express()
 
app.get('/', function (req, res) {
  res.send('Hello World')
})
 
app.listen(3000)

A modern JavaScript utility library delivering modularity, performance & extras

Usage:

$ npm i -g npm
$ npm i --save lodash

MongoDB object modeling tool designed to work in an asynchronous environment

Usage:

$ npm i mongoose

Create friendly url names

Add geocodes from address provided

Middleware for handling file uploads

Usage:

npm i express-fileupload

app.post('/upload', function(req, res) {
  console.log(req.files.foo); // the uploaded file object
});

Styling node.js console logs

Usage:

npm i colors

const colors = require('colors');
console.log('test'.green.inverse);

Usage:

npm i jsonwebtoken

const jwt = require('jsonwebtoken');

UserSchema.methods.getSignedJwtToken = function() {
  return jwt.sign(
    { id: this._id,},
    'theosdasdnadaf',
    { expiresIn: 30d }
  );
};

Password hashing

Usage:

npm i bcryptjs

const bcrypt = require('bcryptjs');

UserSchema.pre('save', async function(next) {
  const salt = await bcrypt.genSalt(10);
  this.password = await bcrypt.hash(this.password, salt);
});

Storing/set tokens on cookies

npm i cookie-parser

// Cookie parser in Server.js file
const express = require('express')
const cookieParser = require('cookie-parser')
 
const app = express()
app.use(cookieParser())

Sending emails

npm i nodemailer

const nodemailer = require('nodemailer');

const sendEmail = async (options) => {
  const transporter = nodemailer.createTransport({
    host: process.env.SMTP_HOST,
    port: process.env.SMTP_PORT,
    auth: {
      user: process.env.SMTP_EMAIL,
      pass: process.env.SMTP_PASSWORD,
    },
  });

  const message = {
    from: `${process.env.FROM_NAME} <${process.env.FROM_EMAIL}>`,
    to: options.email,
    subject: options.subject,
    text: options.message,
  };

  const info = await transporter.sendMail(message);

  console.log('Message sent: %s', info.messageId);
};

module.exports = sendEmail;

Standard node module for cryptographic functionality (Password resets, tokens)

const crypto = require('crypto');

const resetPasswordToken = crypto
 .createHash('sha256')
 .update(req.params.resettoken)
 .digest('hex');

Authentication middleware which support username & password, Facebook and Google

npm install passport

passport.authenticate('facebook')

Most popular chart javascript library for bar charts, scatter charts etc

npm install react-chartjs-2 chart.js

import { Bar } from 'react-chartjs-2';

<Bar
  data={data}
  width={100}
  height={50}
  options={{ maintainAspectRatio: false }}
/>

A modern approach to copy text to clipboard

npm install clipboard

Headless Chrome Node.js API which can be used to control Chrome or Chromium

// Downloads recent version of chromium (~170mb)
npm i puppeteer

//Light version which doesnt download chromium
npm i puppeteer-core

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');
  await page.screenshot({path: 'example.png'});

  await browser.close();
})();

Check for unused dependencies

npm install -g depcheck

depcheck [directory] [arguments]

CLI user prompts

npm install prompts

const prompts = require('prompts');
 
(async () => {
  const response = await prompts({
    type: 'number',
    name: 'value',
    message: 'How old are you?',
    validate: value => value < 18 ? `Nightclub is 18+ only` : true
  });
 
  console.log(response); // => { value: 24 }
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment