Skip to content

Instantly share code, notes, and snippets.

View ganeshkbhat's full-sized avatar

Krishnamurthy G B ganeshkbhat

View GitHub Profile
@ganeshkbhat
ganeshkbhat / expressjs-helmet-csp-index.js
Last active October 2, 2018 15:09
ExpressJS Series: Helmet CSP usage
const express = require('express');
const bodyParser = require('body-parser');
const helmet = require('helmet');
const csp = require('helmet-csp');
let app = express();
// Implementing basic security defaults using helmet
app.use(helmet());
// CSP Options
@ganeshkbhat
ganeshkbhat / expressjs-cors-index.js
Last active October 2, 2018 15:22
ExpressJS Series: Implementing CORS in ExpressJS application
const express = require('express');
const bodyParser = require('body-parser');
const helmet = require('helmet');
const csp = require('helmet-csp');
const cors = require('cors');
let app = express();
// Implementing basic security defaults using helmet
app.use(helmet());
@ganeshkbhat
ganeshkbhat / expressjs-rate-limit-index.js
Created October 2, 2018 15:40
ExpressJS Series: Implementing Simple Rate Limiting for ExpressJS
const express = require('express');
const bodyParser = require('body-parser');
const helmet = require('helmet');
const csp = require('helmet-csp');
const cors = require('cors');
const rateLimit = require("express-rate-limit");
let app = express();
// Implementing basic security defaults using helmet
@ganeshkbhat
ganeshkbhat / expressjs-non-blocking-middleware-snippet.js
Created October 4, 2018 02:46
ExpressJS Series: Non-blocking middleware
const myFirstMiddleware = function(req, res, next) {
 someHTTPRequestLibraryOrPUBSUBLibrary.postOrRequest('https://myexternalloggingserver.com/log', {body: req.path})
.then((data)=>{
  console.log('Sent to logging server', data);
 }, (err) =>{
  console.log('Error Occured, log err to local file', err);
 })
 next();
};
@ganeshkbhat
ganeshkbhat / expressjs-blocking-responding-middleware-snippet.js
Created October 4, 2018 02:48
ExpressJS Series - Blocking Middlewares and Responding from the middleware
const myFirstMiddleware = function(req, res, next) {
 // Http request to server https://myexternalloggingserver.com/log
 someHTTPRequestLibrary.post('https://myexternalloggingserver.com/log', {body: req.path})
.then((data)=>{
  next();
 }, (err) =>{
res.status(500).send('Failed');
 });
};
@ganeshkbhat
ganeshkbhat / expressjs-req.body.snippet.js
Created October 4, 2018 02:54
ExpressJS Series: Getting the req.body
app.post("/someUrl", function(req, res) {
// Content-Type => "application/json" (Header Content-Type in request)
let getContent = req.header('content-type');
if (getContent !== 'application/json') {
res.status(406).send("Not Acceptable");
}
// Raw buffer capture in request body (will be covered later)
const contentBody = JSON.parse(req.body.toString('utf8'));
res.send(200).send({result:"Success", content: contentBody});
});
@ganeshkbhat
ganeshkbhat / expressjs-req.cookies-snippet.js
Created October 4, 2018 02:56
ExpressJS Series: Getting the req.cookies
app.post("/someUrl", function(req, res) {
 // Check if cookie is sent
let session = req.cookie.session;
if (!session) {
res.status(403).send("Forbidden");
}
const contentBody = JSON.parse(req.body.toString('utf8'));
res.send(200).send({result:"Success", content: contentBody});
});
@ganeshkbhat
ganeshkbhat / expressjs-req.fresh.snippet.js
Created October 4, 2018 02:57
ExpressJS Series: Getting the req.fresh
app.post("/someUrl", function(req, res) {
let isFresh = req.fresh;
if (!isFresh) {
res.status(403).send("Forbidden");
}
const contentBody = JSON.parse(req.body.toString('utf8'));
res.send(200).send({result:"Success", content: contentBody});
});
@ganeshkbhat
ganeshkbhat / expressjs-req.hostname.snippet.js
Created October 4, 2018 02:57
ExpressJS Series: Getting the req.hostname
app.post("/someUrl", function(req, res) {
let hostname = req.hostname;
if (hostname !== "xxx") {
res.status(403).send("Forbidden");
}
const contentBody = JSON.parse(req.body.toString('utf8'));
res.send(200).send({result:"Success", content: contentBody});
});
@ganeshkbhat
ganeshkbhat / expressjs-req.ip.snippet.js
Last active October 4, 2018 02:59
ExpressJS Series: Getting the req.ip
app.post("/someUrl", function(req, res) {
let whitelistIPAddress = req.ip;
if (whitelistIPAddress !== "192.168.0.4") {
res.status(403).send("Forbidden");
}
const contentBody = JSON.parse(req.body.toString('utf8'));
res.send(200).send({result:"Success", content: contentBody});
});