Skip to content

Instantly share code, notes, and snippets.

View vkarpov15's full-sized avatar

Valeri Karpov vkarpov15

View GitHub Profile
@vkarpov15
vkarpov15 / gist:c58b1cabb5250e4c999c
Created August 25, 2014 21:55
15 line (but not durable) HTTP load balancer with NodeJS
var http = require('http');
var request = require('request');
var addrs = [
'http://127.0.0.1:3000',
'http://127.0.0.1:3001'
];
var index = 0;
http.createServer(function(req, res) {
@vkarpov15
vkarpov15 / hanging_nextobject
Created January 13, 2015 21:03
Hanging nextObject() call
// MongoDB 2.6, Linux Mint 17.1, node driver HEAD
/* Output looks like:
```
nextObject returned
Calling nextObject again...
Calling execGetMore
```
The `console.log('second nextObject returned');` line never gets executed and the program exits because of no listeners
@vkarpov15
vkarpov15 / gateway.js
Created July 11, 2017 18:56
Consolidated API keys with express gateway
const Keen = require('keen-js');
const Mailgun = require('mailgun-js');
const bodyParser = require('body-parser');
const express = require('express');
const app = express();
app.use(bodyParser.json());
const keys = {
// Top-level is the gateway's API keys, so user will send `token-1` to the
@vkarpov15
vkarpov15 / eg-oauth.md
Last active September 18, 2019 00:26
Brief guide to setting up oauth with Express gateway

Implementing Oauth in Express Gateway

Express Gateway gives you the ability to spin up your own oauth provider from the command line. Oauth enables your users to delegate API endpoints to various apps via scopes. In this introductory article, you'll learn how to get up and running with Oauth in Express Gateway.

Configuring the Gateway

Express Gateway comes with a lot of powerful features baked in, like OAuth2 and key auth. When built-in features aren't enough, Express Gateway has an expression policy, which lets you execute arbitrary JavaScript to modify the request and response. In this article, I'll show you how the expression policy works with several sample use cases.

Hello Expressions

To get started, let's create a basic expression: one that returns an

Express Gateway has built-in support for numerous authentication mechanisms, like OAuth2 and key auth. On top of these authentication mechanisms, Express Gateway supports restricting access to certain endpoints to certain users using the notion of scopes. In this article, I'll provide a "Hello, World" example of using scopes and then dive into a more realistic example of using scopes to protect access to an external API.

Hello, Scopes

Express Gateway has a lot of powerful features beyond just auth. Another important feature is rate limiting, which throttles requests to one or more endpoints. Express Gateway has a lot of tuneable options for configuring throttling: you can throttle requests on a per user, per endpoint, or per pipeline basis. In this article, I'll walk you through a "Hello, World" example of using Express Gateway's rate limiting policy, and then show a practical use case of rate limiting based on user API keys.

Intro to the Rate Limiting Policy

@vkarpov15
vkarpov15 / example.js
Last active October 26, 2017 22:55
Consistent Arrays from Query Params with Express and Archetype
const Archetype = require('archetype');
const express = require('express');
const superagent = require('superagent');
const app = express();
const Params = new Archetype({
names: { $type: ['string'], $default: [] }
}).compile('Params');
app.get('/', function(req, res) {
@vkarpov15
vkarpov15 / archetype-20171013-embedded-object.js
Created October 13, 2017 17:24
Embedded Objects vs Embedded Types in Archetype
const Archetype = require('archetype');
const assert = require('assert');
const RequestType = new Archetype({
service: {
$type: 'string',
$required: true
},
// `location` is implicitly `$required` because
// `type` and `coordinates` are `$required`
@vkarpov15
vkarpov15 / archetype-20171020-pick.js
Created October 20, 2017 23:59
Embedding a Subset of One Archetype's Properties in another Archetype
const Archetype = require('archetype');
// Simplified customer type for the sake of example. This customer
// type has 2 top-level properties: a string `email` and an object
// `name` that has nested string properties `first` and `last`.
const CustomerType = new Archetype({
name: {
first: { $type: 'string' },
last: { $type: 'string' }
},