Skip to content

Instantly share code, notes, and snippets.

View deepal's full-sized avatar

Deepal Jayasekara deepal

  • CompareTheMarket.com
  • London, United Kingdom
View GitHub Profile
@deepal
deepal / nodesec-error-handling-async.js
Created February 17, 2016 18:15
nodesec-error-handling-async.js
myAsyncFunction(somedata, function(err, response){
if (err){
/* handle this error */
}
else{
/* do something with response */
}
});
@deepal
deepal / nodesec-mongodb-injection-server.js
Last active February 17, 2016 19:00
nodesec-mongodb-injection
DBSecretNotes.find({username: req.body.username, secret: req.body.secret}).exec(function(err, secretNotes){
//List all secret notes of the user
});
@deepal
deepal / nodesec-hpp.js
Created February 18, 2016 11:36
nodesec-hpp
var hpp = require('hpp');
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({extended: false})); //body parser should be placed before using hpp
app.use(hpp());
app.get('/', function(req, res, next){
console.log('Query Parameters : ' + JSON.stringify(req.query));
{
"name": "My App",
"version": "0.0.0",
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "~1.13.2",
"cookie-parser": "~1.3.5",
"debug": "~2.2.0",
@deepal
deepal / nodesec-secrets-in-env.js
Last active February 18, 2016 18:07
nodesec-secrets-in-env
app.use(session({
name: 'SESS_ID',
secret: process.env.EXPRESS_SESSION_SECRET,
resave: false,
saveUninitialized: true,
cookie: {
secure: true,
httpOnly: true
}
}));
var express = require('express');
var http = require('http');
var app = express();
/*define routes here*/
var server = http.createServer(app);
server.listen(80, function(err){
if (err) {
@deepal
deepal / angular-inject.js
Created February 22, 2016 09:12
Using $inject to inject dependencies in AngularJS
angular.module('app')
.controller('MyController', MyController);
MyController.$inject = ['$scope', '$http', '$location'];
function MyController ($scope, $http, $location) {
// write controller function
}
@deepal
deepal / angular-routehelper.js
Created February 22, 2016 09:18
Using routeHelper to register angular routes
angular.module('app')
.run(['routeHelper', function(routeHelper){
routeHelper.configureRoutes(getRoutes());
}]);
function getRoutes() {
return [
{
url: '/',
config: {
// Nodejs encryption with GCM
// Does not work with nodejs v0.10.31
// Part of https://github.com/chris-rock/node-crypto-examples
var crypto = require('crypto'),
algorithm = 'aes-256-gcm',
password = '3zTvzr3p67VC61jmV54rIYu1545x4TlY',
// do not use a global iv for production,
// generate a new one for each encryption
iv = '60iP0h6vJoEa'
const express = require('express');
const bodyParser = require('body-parser');
const database = require('../database');
const app = express();
app.use(bodyParser.json());
app.get('/list', function (req, res) {
const todos = database.getToDoList();
res.status(200).send(todos);
});