Skip to content

Instantly share code, notes, and snippets.

View WaleedAshraf's full-sized avatar
:octocat:
Focusing

Waleed Ashraf WaleedAshraf

:octocat:
Focusing
View GitHub Profile
@WaleedAshraf
WaleedAshraf / mock-session-4.js
Last active March 18, 2018 19:06
Mock Express Session
exports.submit = function(req, res) {
if(req.session.count && req.session.count > 0) { // true
//do something
res.sendStatus(200);
}
}
@WaleedAshraf
WaleedAshraf / mock-session-3.js
Last active March 18, 2018 19:07
Mock Express Session
// name = "my-session" -> base64 value of required object (cookie)
// name.sig = "my-session.sig" -> signed value of cookie using Keygrip
let cookie = Buffer.from(JSON.stringify({"count":2})).toString('base64'); // base64 converted value of cookie
let kg = Keygrip(['testKey']) // same key as I'm using in my app
let hash = kg.sign('my-session=' + cookie);
await request(app).get('api/image/submit').set("Accept", "text/html")
.set('cookie', ['my-session=' + cookie + '; ' + 'my-session.sig=' + hash + ';'])
@WaleedAshraf
WaleedAshraf / mock-session-2.js
Last active March 18, 2018 19:07
Mock Express Session
exports.submit = function(req, res) {
if (req.session.count && req.session.count > 0) { // get count from req.session
// perform further tasks
res.sendStatus(200);
} else {
res.sendStatus(400);
}
}
@WaleedAshraf
WaleedAshraf / mock-session-1.js
Last active March 18, 2018 19:07
Mock Express Session
exports.create = function(req, res) {
// upload image to s3
var photos = PhotosTable.create(req.files.names); // add entries in database
req.session.count = photos.length; // set count in req.session
res.redirect(`api/image/submit`);
}
@WaleedAshraf
WaleedAshraf / smsWorker.js
Created March 23, 2018 17:26
Switching from cluster module to PM2 & RabbitMQ
process.on('message', function(data) {
sendSMS(data); // use twilio or something to send sms
});
@WaleedAshraf
WaleedAshraf / cluster-module.js
Last active March 25, 2018 13:21
Switching from cluster module to PM2 & RabbitMQ
var cluster = require('cluster');
const numCPUs = require('os').cpus().length;
module.exports.create = function(options, callback){
if (cluster.isMaster) {
// fork child process for notif/sms/email worker
global.smsWorker = require('child_process').fork('./smsWorker');
global.emailWorker = require('child_process').fork('./emailWorker');
global.notifiWorker = require('child_process').fork('./notifWorker');
var pm2Config = {
"apps": [
{
"name": "app",
"script": "app.js",
"exec_mode": "cluster_mode",
"instances": "max"
},
{
"name": "smsWorker",
@WaleedAshraf
WaleedAshraf / smsWorker.js
Created March 25, 2018 19:04
Switching from cluster module to PM2 & RabbitMQ
// exports RabbitMQ connection
const MQ = require('./rabbitmq-config');
MQ.on('sms', function(data) {
sendSMS(data); // use twilio or something to send sms
});
@WaleedAshraf
WaleedAshraf / serverless-app-4.js
Created April 19, 2018 13:00
Building you first Serverless app in Node.js with AWS Lambda + S3 + API Gateway
{
"Version": "2012-10-17",
"Id": "Policy1522618459372",
"Statement": [
{
"Sid": "Stmt1522618452845",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:*",
"Resource": "arn:aws:s3:::foobucketlambda/*"
@WaleedAshraf
WaleedAshraf / serverless-app-5.js
Last active April 20, 2018 20:56
Building you first Serverless app in Node.js with AWS Lambda + S3 + API Gateway
const im = require('imagemagick');
const fs = require('fs');
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
exports.handler = async (event) => {
const operation = event.queryStringParameters ? event.queryStringParameters.operation : null;
let data = JSON.parse(event.body);
switch (operation) {
case 'ping':