Skip to content

Instantly share code, notes, and snippets.

View c4x1's full-sized avatar
🌀
home->work->home->work;

CSpornov c4x1

🌀
home->work->home->work;
  • Russian
View GitHub Profile
@c4x1
c4x1 / 000-server.js
Created July 28, 2019 14:48 — forked from netroy/000-server.js
Using cluster to scale existing Express/Connect apps
var cluster = require('cluster'),
app = require('./app');
var workers = {},
count = require('os').cpus().length;
function spawn(){
var worker = cluster.fork();
workers[worker.pid] = worker;
return worker;
@c4x1
c4x1 / app.js
Created July 28, 2019 14:47 — forked from Ajido/app.js
nodejs express4 cluster graceful restart / shutdown
...
app.use(function(req, res, next){
if (app.get('graceful_shutdown') === true) {
res.set('Connection', 'close');
}
next();
});
app.set('graceful_shutdown_start', function() {
@c4x1
c4x1 / server.js
Created July 28, 2019 14:44 — forked from tbranyen/server.js
backbone/node.js pushState enabled server
// Require libraries
var os = require("os");
var fs = require("fs");
var readline = require("readline");
var cluster = require("cluster");
var express = require("express");
var site = express();
// Var up, bro
var i, read;
@c4x1
c4x1 / Node.js clustered HTTP server example.js
Last active August 3, 2019 10:37 — forked from dsibilly/gist:2992412
Node.js clustered HTTP server example
(function () {
'use strict';
var cluster = require('cluster'),
http = require('http'),
os = require('os'),
/*
* ClusterServer object
@c4x1
c4x1 / promise-dot-all.js
Created July 24, 2019 01:16 — forked from indiesquidge/promise-dot-all.js
Recreating Promise.all with async/await
/*
Let us re-create `Promise.all`
`Promise.all` method returns a promise that resolves when all of the promises in the iterable argument have resolved,
or rejects with the reason of the first passed promise that rejects.
Read more about `Promise.all` on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
A basic example would be something like this:
async function selectPizza() {
 const pizzaData = await getPizzaData()    // асинхронный вызов
 const chosenPizza = choosePizza()    // синхронный вызов
 await addPizzaToCart(chosenPizza)    // асинхронный вызов
}
async function selectDrink() {
 const drinkData = await getDrinkData()    // асинхронный вызов
 const chosenDrink = chooseDrink()    // синхронный вызов
 await addDrinkToCart(chosenDrink)    // асинхронный вызов
@c4x1
c4x1 / async-examples.js
Created July 24, 2019 00:32 — forked from developit/async-examples.js
Async Array utilities in async/await. Now available as an npm package: https://github.com/developit/asyncro
/** Async version of Array.prototype.reduce()
* await reduce(['/foo', '/bar', '/baz'], async (acc, v) => {
* acc[v] = await (await fetch(v)).json();
* return acc;
* }, {});
*/
export async function reduce(arr, fn, val, pure) {
for (let i=0; i<arr.length; i++) {
let v = await fn(val, arr[i], i, arr);
if (pure!==false) val = v;
@c4x1
c4x1 / async.parallel.js
Last active July 24, 2019 00:29
Created with Copy to Gist
async.parallel({
friends : function(cb) { getFriends(cb); },
messages : function(cb) { getMessages(cb); },
notifications : function(cb) { getNotifications(cb); }
}, function(e, results) {
if( e ) handleError(e);
doSomethingWith(results);
});
'use restrict';
/**
* Module dependences.
*/
var mongoose = require('mongoose');
/**
* load up the material model.
*/
@c4x1
c4x1 / 1563520018.js
Last active July 19, 2019 07:12
madhums/node-express-mongoose-demo
'use strict';
/**
* Module dependencies.
*/
const mongoose = require('mongoose');
const notify = require('../mailer');
// const Imager = require('imager');