Skip to content

Instantly share code, notes, and snippets.

View cookie-ag's full-sized avatar

Cookie AG cookie-ag

View GitHub Profile
@cookie-ag
cookie-ag / async_usecase01.js
Created September 12, 2016 03:26
Simple use of async parallel to find user and count them
/*-------------- Simple use of async parallel to find user and count them --------------*/
var NoticiaModel = require('../models/noticias');
var User = require('../models/user');
var ObjectId = require('mongoose').Types.ObjectId;
var async = require('async');
exports.index = function(req, res) {
async.parallel([
/*-------------- find, n=1 --------------*/
@cookie-ag
cookie-ag / TestNewDeployment.js
Last active October 5, 2016 05:30
Simple Hello world to use when setting up production server
#!/usr/bin/env nodejs
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080, 'localhost');
console.log('Server running at http://localhost:8080/');
@cookie-ag
cookie-ag / default
Last active October 4, 2016 05:18
Nginx config for Ubuntu 16.04
// /etc/nginx/sites-available/default
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
@cookie-ag
cookie-ag / default
Created October 4, 2016 05:29
Nginx with HTTPS
# HTTP - redirect all requests to HTTPS:
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
return 301 https://$host$request_uri;
}
# HTTPS - proxy requests on to local Node.js app:
server {
listen 443;
@cookie-ag
cookie-ag / app.js
Created October 4, 2016 06:04
Simple HTTPS express 4.x server
// Create self-signed certificate using $ openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365
// Key is certificate key and cert.pem is certificate file
var fs = require('fs'),
https = require('https'),
express = require('express'),
app = express();
https.createServer({
key: fs.readFileSync('key.pem'),
@cookie-ag
cookie-ag / app.js
Created October 4, 2016 06:26
Sample HTTPS server for Express 4.x
// Create self-signed certificate using:
// openssl genrsa 1024 > private.key
// openssl req -new -key private.key -out cert.csr
// openssl x509 -req -in cert.csr -signkey private.key -out certificate.pem
// Key is certificate key and cert.pem is certificate file
// 443 is standard for all HTTPS but is not recommended to use as it might be obvious port to attack.
var fs = require('fs');
var https = require('https');
var express = require('express');
@cookie-ag
cookie-ag / config.txt
Last active October 5, 2016 06:03
Creating a safe user in Ubuntu 16.04
// If the application has a error it can bring down the complete server as it has credentials to do so.
// To prevent this : Create a new user in /home/safeuser/
- if [[ $(id -u) -ne 0 ]] ; then echo "Please run as root" ; exit 1 ; fi (Check if you are root)
- sudo -i (change to root)
- cut -d: -f1 /etc/passwd (To list all local users)
- useradd -s /bin/bash -m -d /home/safeuser -c "safe user" safeuser :(Create only as root)
- passwd safeuser : (Add a password)
- usermod -aG sudo safeuser :(Give safeuser permission to use root level commands)
- Login as safeUser : ssh safeuser@IP
@cookie-ag
cookie-ag / config.txt
Last active July 18, 2017 10:16
Giving safeuser permission to access port 80
// Since safeuser is not admin, it cannot access port 80 permissions.
// For obvious reasons, we don't want root to access the node.js in production
- sudo apt-get install libcap2-bin
# Non nodesource distribution
- which node
- sudo setcap cap_net_bind_service=+ep /usr/local/bin/node
# Nodesource distribution
@cookie-ag
cookie-ag / Register_controller.js
Last active January 18, 2017 09:14
U2F WorkFlow with MongoDB : Registering and Verification
var appId = 'https://127.0.0.1'
var routine = require('routine');
exports.GET = function(req, res, next) {
var tokens = req.session.tokens || [];
routine.U2FstartRegistration(req, res, next, appId, tokens);
};
exports.POST = function(req, res, next) {
var request = req.session.registrationRequest;
@cookie-ag
cookie-ag / HTTPReq.js
Last active September 7, 2017 04:31
Query logging database (MongoDB)
db.getCollection('Logging').count({"status":{$eq: 200}}) //Equal to 200
db.getCollection('Logging').count({"status":{$gt: 399}}) //Greater than to 399, HTTP Errors
db.getCollection('Logging').count({"status":{$lt: 399}}) //Less than 399, HTTP Success