Skip to content

Instantly share code, notes, and snippets.

// This function will return a promise that will fetch data from a certain URL
function getURLPromise(url) {
return new Promise((resolve, reject) => {
http.get(url, (rs) => {
var data = ''
rs.on('data', (chunk) => data += chunk);
rs.on('end', ()=> resolve(data.length));
});
});
co(function* () {
var result = yield Promise.resolve(true);
return result;
}).then(function (value) {
console.log(value);
}, function (err) {
console.error(err.stack);
});
co(function *() {
// yield any promise
var a = yield getURLPromise('http://www.google.com')
var b = yield getURLPromise('http://www.apple.com')
var c = yield getURLPromise('http://www.microsoft.com')
debug([a, b, c]);
}).catch((error) =>{
debug("Something went wrong: ", error)
});
div.login-page
form(class='form-login', action='/web-login', method='POST')
input(type='text', name='username', placeholder='Username',required, autofocus)
input(type='password', name='password', placeholder='Password', required)
input(type='hidden', id='url', name='url')
button(class='btn', type='submit') Sign in
script(type='text/javascript').
document.getElementById("url").value = window.location.hash;
// Login strategy. Enable passing through of the req object by setting passReqToCallback to true.
passport.use(new Strategy({ passReqToCallback: true },
function (req,username, password, done) {
let user = your_user_auth(username,password); // returns the user object if valid, otherwise null
if(user)
// Extract the redirect hash from the request body and store it in the request session object
req.session.redirect_url = req.body.url;
return done(null, user);
}
return done(null, false);
// User requests the /app/ route -> Check if he is authenticated
router.get('/app/', isAuthenticated, (req, res) => res.render('app') );
// The isAuthenticated function is using passport.js here to see if the user is logged in (req.isAuthenticated)
// It that's not the case, we redirect them to the login route.
const isAuthenticated = (req, res, next) => {
if(req.isAuthenticated()) return next();
res.redirect('/login');
}
@Qaaj
Qaaj / reselect.js
Created September 7, 2017 20:22
Reselect Playground
var createStructuredSelector = require('reselect').createStructuredSelector;
var createSelector = require('reselect').createSelector;
// Example of structured selector
const mySelectorA = (state) => state.first_user;
const mySelectorB = (state) => state.second_user;
const mySelectorC = (state) => state.second_user + state.first_user;
const structuredSelector = createStructuredSelector({
@Qaaj
Qaaj / basic-express-ethereum.js
Created November 23, 2017 18:58
Displaying coinbase of Ethereum network
const express = require('express');
const Web3 = require('web3');
const app = express();
const web3 = new Web3(new Web3.providers.HttpProvider("http://geth:8545"));
app.get('/', async (req, res) => res.send(`Coinbase: ${await web3.eth.getCoinbase()}`));
app.listen(3001, () => console.log('Listening on port 3001!'));
@Qaaj
Qaaj / Dockerfile
Last active November 26, 2017 13:40
Live-reload dockerfile
FROM node:7.8.0
ENV NPM_CONFIG_LOGLEVEL warn
WORKDIR /app
COPY package.json ./
RUN npm install
RUN npm install -g nodemon
// Tag and custom dockerfile
docker build -t cryptotracker . -f Dockerfile.txt
docker container prune
// ENV file
docker run -e REDIS_URL='...' --env-file vars -p 3001:3001 cryptotracker
// Networks
docker network list
docker network create <network>