Skip to content

Instantly share code, notes, and snippets.

View anshulrgoyal's full-sized avatar
🏠
Working from home

Anshul Goyal anshulrgoyal

🏠
Working from home
View GitHub Profile
@anshulrgoyal
anshulrgoyal / httpstring.txt
Last active December 19, 2018 15:20
HTTP TEXT BASED PROTOCOL INPUT STRING
POST /tokens HTTP/1.1\r\n
Host: localhost:4443\r\n
Content-Type: application/json\r\n
Content-Length: ${length}\r\n
token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
\r\n\r\n
${stringBody}
\r\n
@anshulrgoyal
anshulrgoyal / server.js
Created December 19, 2018 16:25
Server Example
"use strict";
//Imports
const http = require("http");
const url = require("url");
const queryString = require("querystring");
const StringDecoder = require("string_decoder").StringDecoder;
// ports for the process
const httpPort = 4443;
@anshulrgoyal
anshulrgoyal / example.js
Last active December 19, 2018 17:17
Handler Example
const handler = {
// if path is localhost:3000/user
user: (data, cb) => {
const {
method
} = data;
const allowedMethods = ["post", "get"];
if (allowedMethods.indexOf(method) !== -1) {
handler._count[method](data, cb);
@anshulrgoyal
anshulrgoyal / compressAgain.js
Created December 21, 2018 09:00
Create file from compressed data
const fs = require("fs");
const zlib = require("zlib");
// create stream for compressing data
const compressedDataStream = zlib.createDeflate();
// create a stream to write file to fs
const writeStreamFile = fs.createWriteStream(
__dirname + "/index-compressed.html"
);
// reading file original file from disk
@anshulrgoyal
anshulrgoyal / compressAgainAgian.js
Created December 24, 2018 11:44
How to compress a response
const http = require("http");
const fs = require("fs");
const zlib = require("zlib");
const server = http.createServer();
server.on("request", (req, res) => {
const data = "random string";
const encodingHeader = req.headers["accept-encoding"];
@anshulrgoyal
anshulrgoyal / saveToExample.js
Created December 24, 2018 13:50
Save file to directory
const fs=require('fs');
create = (dir, record, data, cb = () => {}) => {
// checking if collection directory exists or not
const isDir = fs.existsSync(path.resolve(`${__dirname}/../.data/${dir}`))
if (isDir) {
// if yes let us write the file
fs.writeFile(path.resolve(`${__dirname}/../.data/${dir}/${record}.json`), JSON.stringify(data),
(err) => {
if (err) cb(err);
else cb(null, data);
@anshulrgoyal
anshulrgoyal / readFromMemory.js
Created December 26, 2018 10:08
Make the tree of all the data in memory
const fs=require('fs');
const path=require('path');
fs.readdirSync(path.resolve('./.data')).forEach((dir) => {
db[dir] = {};
fs.readdirSync(path.resolve('./.data/' + dir)).forEach((file) => {
db[dir][file.split('.')[0]] = JSON.parse(fs.readFileSync(path.resolve('./.data/' + dir + '/' +
file)));
})
})
@anshulrgoyal
anshulrgoyal / request.js
Created December 26, 2018 10:27
Make a request
const https = require('https');
const URL = require('url');
const queryString = require('querystring')
ajax = (url, options = {}, cb) => {
options = {
method: 'get',
body: null,
...options
}
@anshulrgoyal
anshulrgoyal / app.js
Last active December 27, 2018 18:09
Simple server for verifying json web token
const express=require('express');
const jwt=require('jsonwebtoken');
const user=require('./user');
const key=require("./key");
const app=express();
app.use(require('body-parser').json());
app.use(function(req,res,next){
try{
@anshulrgoyal
anshulrgoyal / webpack.config.js
Created January 6, 2019 07:01
WebPack Config File
const path = require("path");
module.exports = {
entry: "./src/index.js",
mode: "development",
output: {
filename: "./main.js"
},
devServer: {
contentBase: path.join(__dirname, "dist"),