Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

cleanup and comments in examples

  • Loading branch information...
commit 3275433062aaea15810057f9482df8fd85bcfac5 1 parent e227f5c
@mashpie authored
View
41 examples/express-cookies/express-cookies.js
@@ -1,23 +1,32 @@
+/**
+ * This example is intended to show a cookie usage in express setup and
+ * also to be run as integration test for concurrency issues.
+ *
+ * Please remove setTimeout(), if you intend to use it as a blueprint!
+ *
+ */
+
+// require modules
var express = require('express'),
i18n = require('../../i18n'),
+ url = require('url'),
app = module.exports = express();
+// minimal config
i18n.configure({
locales: ['en', 'de'],
cookie: 'yourcookiename',
directory: __dirname+'/locales',
});
-// app.initI18n = function (req, res) {
-// res.__ = function () {
-// return i18n.__.apply(req, arguments);
-// };
-// i18n.init(req, res);
-// };
-
app.configure(function () {
+ // you'll need cookies
app.use(express.cookieParser());
+
+ // init i18n module for this loop
app.use(i18n.init);
+
+ // register helper to res
app.use(function (req, res, next) {
res.locals.__ = res.__ = function () {
return i18n.__.apply(req, arguments);
@@ -31,19 +40,25 @@ app.configure(function () {
});
app.get('/test', function (req, res) {
- var _ref = 0,
- delay = (_ref = req.query.delay) != null ? _ref : 0;
+ // delay a response to simulate a long running process,
+ // while another request comes in with altered language settings
return setTimeout(function () {
res.send('<body>' + res.__('Hello') + '</body>');
- }, delay);
+ }, app.getDelay(req, res));
});
app.get('/testfail', function (req, res) {
- var _ref = 0,
- delay = (_ref = req.query.delay) != null ? _ref : 0;
+ // delay a response to simulate a long running process,
+ // while another request comes in with altered language settings
return setTimeout(function () {
res.send('<body>' + i18n.__('Hello') + '</body>');
- }, delay);
+ }, app.getDelay(req, res));
});
+// simple param parsing
+app.getDelay = function (req, res) {
+ return url.parse(req.url, true).query.delay || 0;
+}
+
+// startup
app.listen(3000);
View
12 examples/express-cookies/test.js
@@ -8,21 +8,21 @@ EN.cookies('localhost', '/').set("yourcookiename", "en");
DE.cookies('localhost', '/').set("yourcookiename", "de");
describe('res.__() is able to handle concurrent request correctly', function(){
- describe('serial requests return different languages', function () {
+ describe('serial requests', function () {
visitLinks('series', 'test', EN, 'Hello', DE, 'Hallo');
});
- describe('parallel requests return different languages', function () {
- visitLinks('series', 'test', EN, 'Hello', DE, 'Hallo');
+ describe('parallel requests', function () {
+ visitLinks('parallel', 'test', EN, 'Hello', DE, 'Hallo');
});
})
describe('i18n.__() is NOT able to handle concurrent request correctly', function(){
- describe('serial requests return different languages', function () {
+ describe('serial requests', function () {
visitLinks('series', 'testfail', EN, 'Hello', DE, 'Hello');
});
- describe('parallel requests return different languages', function () {
- visitLinks('series', 'testfail', EN, 'Hello', DE, 'Hello');
+ describe('parallel requests', function () {
+ visitLinks('parallel', 'testfail', EN, 'Hello', DE, 'Hello');
});
})
View
14 examples/node-http/node-http.js
@@ -1,3 +1,11 @@
+/**
+ * This example is intended to show a basic plain vanilla setup and
+ * also to be run as integration test for concurrency issues.
+ *
+ * Please remove setTimeout(), if you intend to use it as a blueprint!
+ *
+ */
+
// require modules
var http = require('http'),
i18n = require('../../i18n'),
@@ -12,13 +20,13 @@ i18n.configure({
// simple server
app = http.createServer(function (req, res) {
- var delay = app.getDelay(req, res),
- message;
+ var delay = app.getDelay(req, res);
// init & guess, see hepler below
app.i18n(req, res);
- // delay a response to let global i18n setting be altered meanwhile, => 'Hallo' with Accept-Languag: de
+ // delay a response to simulate a long running process,
+ // while another request comes in with altered language settings
setTimeout(function () {
res.end(res.__('Hello'));
}, delay);
View
6 examples/node-http/test.js
@@ -15,11 +15,11 @@ var Browser = require('zombie'),
});
describe('res.__() is able to handle concurrent request correctly', function(){
- describe('serial requests return different languages', function () {
+ describe('serial requests', function () {
visitLinks('series', 'test', EN, 'Hello', DE, 'Hallo');
});
- describe('parallel requests return different languages', function () {
- visitLinks('series', 'test', EN, 'Hello', DE, 'Hallo');
+ describe('parallel requests', function () {
+ visitLinks('parallel', 'test', EN, 'Hello', DE, 'Hallo');
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.