Skip to content

Instantly share code, notes, and snippets.

View heytulsiprasad's full-sized avatar
⚛️
Overreacting

Tulsi Prasad heytulsiprasad

⚛️
Overreacting
View GitHub Profile
@heytulsiprasad
heytulsiprasad / errorHandler.js
Created July 17, 2020 21:10
Basic express server setup
// src/middleware/errorHandler.js
// Not found middleware
// 404
const notFound = (req, res, next) => {
const error = new Error(`Not Found - ${req.originalUrl}`);
res.status(404);
// pass error to the next middleware
@heytulsiprasad
heytulsiprasad / howToRef.md
Created July 19, 2020 14:09
How to use refs in react?

In Class Components

To create the ref

class Forest extends React.Component {
  constructor(props) {
    super(props)
    this.plantRef = React.createRef()
 }
@heytulsiprasad
heytulsiprasad / module.css
Created July 19, 2020 19:20
Keep navbar and footer fixed at their position (irrespective of content)
.Home {
display: flex;
min-height: 100vh;
flex-direction: column;
}
.HomeContent {
flex-grow: 1;
}
@heytulsiprasad
heytulsiprasad / upload.js
Created July 20, 2020 07:40 — forked from virolea/upload.js
Tracking file upload progress using axios
upload(files) {
const config = {
onUploadProgress: function(progressEvent) {
var percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total)
console.log(percentCompleted)
}
}
let data = new FormData()
data.append('file', files[0])
@heytulsiprasad
heytulsiprasad / displayImage.js
Created July 20, 2020 08:44
How to display a selected image from user environment?
// file: selected/dropped by user
function displayImage(file) {
const reader = new FileReader()
reader.readAsDataURL(file) // reader.result will be `data:__`
reader.onload = (e) => imageRef.current.src = reader.result
console.log(reader.result) // returns `data:__` which is URL of files data
}
@heytulsiprasad
heytulsiprasad / settings.json
Created July 20, 2020 19:52
How to remove debug status icon from package.json?
{
"debug.javascript.codelens.npmScripts": "never",
}
@heytulsiprasad
heytulsiprasad / copyToClip.js
Last active July 22, 2020 09:23
How to copy to clipboard and paste?
import React, { Component, createRef } from "react";
class Input extends Component {
constructor(props) {
super(props);
this.state = {
name: "",
copyMsg: "Copy to Clipboard",
isCopied: false,
};
@heytulsiprasad
heytulsiprasad / react-local-setup.md
Last active July 23, 2020 07:19
Error I faced while setting up reactjs.org locally.
ERROR #85923  GRAPHQL

There was an error in your GraphQL query:

Cannot query field "allExampleCode" on type "Query".

If you don't expect "allExampleCode" to exist on the type "Query" it is most likely a typo.
However, if you expect "allExampleCode" to exist there are a couple of solutions to common problems:
@heytulsiprasad
heytulsiprasad / passport.js
Last active July 23, 2020 18:25
How to create a passport jwt strategy?
// Creating a passport strategy
const JwtStrategy = require("passport-jwt").Strategy;
const ExtractJwt = require("passport-jwt").ExtractJwt;
const mongoose = require("mongoose");
// Get the user model
const User = mongoose.model("users");
const keys = require("../config/keys");
const opts = {};
@heytulsiprasad
heytulsiprasad / register.js
Last active July 24, 2020 17:43
Using validator in express backend
const Validator = require("validator");
const isEmpty = require("./is-empty");
// we cant use isEmpty from validator pkg as it needs its param to be a string
// data is req.body
module.exports = function validateRegisterInput(data) {
let errors = {};
data.name = isEmpty(data.name) ? "" : data.name;
data.email = isEmpty(data.email) ? "" : data.email;