-
-
Save acroca/7228226 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var express = require('express'); | |
var connect = require('connect'); | |
var http = require('http'); | |
var newrelic = require('newrelic'); | |
function allowCrossDomain(req, res, next) { | |
res.setHeader('Access-Control-Allow-Origin', '*'); | |
next(); | |
} | |
function time_logger(req, res, next) { | |
res.start_time = new Date; | |
res.on('header', function() { | |
res.set('X-Response-Time', new Date - res.start_time); | |
}); | |
next(); | |
} | |
function bodyParser(req, res, next) { | |
if (!connect.utils.hasBody(req)) { return next(); } | |
// The test never has body, so this will never happen. | |
} | |
function signer(req, res, next) { | |
// Simulates an application fetch | |
setTimeout(function(){ | |
req.application = "123"; | |
next(); | |
}, 10) | |
} | |
function authentication(req, res, next) { | |
// Simulates a user fetch | |
setTimeout(function(){ | |
req.current_user_id = 123 | |
next(); | |
}, 10); | |
} | |
var errors = { | |
middleware : function (err, req, res, next) { | |
console.log('lol:', err.message); | |
res.end("ERROR", 500) | |
} | |
}; | |
var controllers = { | |
user : { | |
show : function (req, res) { | |
console.log("===> User show action "); | |
res.json({name: "Username"}, 200); | |
} | |
} | |
}; | |
var app = express(); | |
app.use(allowCrossDomain); | |
var user_show_middlewares = [ | |
time_logger, | |
bodyParser, | |
signer, | |
authentication, | |
function (req, res, next) { | |
newrelic.setControllerName('users', 'show'); | |
next(); | |
}, | |
controllers.user.show | |
]; | |
app.get('/v1/users/:id', user_show_middlewares); | |
app.use(errors.middleware); | |
http.createServer(app).listen(8080, function () { | |
console.log("listening on port 8080"); | |
}); | |
setTimeout(function(){ | |
setInterval(function(){ | |
http.get("http://localhost:8080/v1/users/123") | |
}, 100) | |
setInterval(function(){ | |
http.get("http://localhost:8080/v1/users/123") | |
}, 100) | |
setInterval(function(){ | |
http.get("http://localhost:8080/v1/users/123") | |
}, 100) | |
setInterval(function(){ | |
http.get("http://localhost:8080/v1/users/123") | |
}, 100) | |
setInterval(function(){ | |
http.get("http://localhost:8080/v1/users/123") | |
}, 100) | |
setInterval(function(){ | |
http.get("http://localhost:8080/v1/users/123") | |
}, 100) | |
}, 100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment