Skip to content

Instantly share code, notes, and snippets.

@ChuckJHardy
Created September 28, 2013 16:19
Show Gist options
  • Save ChuckJHardy/6743683 to your computer and use it in GitHub Desktop.
Save ChuckJHardy/6743683 to your computer and use it in GitHub Desktop.
Express Server for ZeroMQ, Socket.io and Angular.js
'use strict';
var express = require('express'),
app = express(),
http = require('http'),
server = http.createServer(app),
path = require('path'),
io = require('socket.io').listen(server),
fs = require('fs'),
zmq = require('zmq'),
receiver = zmq.socket('pull');
// Assign port for all environments
app.set('port', process.env.PORT || 3000);
app.use(express.favicon());
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
// Marker for `grunt-express` to inject static folder/contents
app.use(function staticsPlaceholder(req, res, next) {
return next();
});
app.configure('development', function() {
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
app.use(express.static(path.join(__dirname, 'app')));
app.use(express.logger('dev'));
});
app.configure('production', function() {
app.use(express.static(path.join(__dirname, 'dist')));
app.use(express.errorHandler());
});
io.configure( function(){
io.set('log level', 3);
io.set('transports', ['websocket']);
});
io.sockets.on('connection', function (socket) {
receiver.on('message', function(message) {
socket.emit('marker', { 'message': escape(message) });
});
});
exports.use = function() {
app.use.apply(app, arguments);
};
receiver.bindSync("tcp://*:5558");
server.listen(app.get('port'), function () {
console.log('Express server listening on port %d in %s mode', app.get('port'), app.get('env'));
});
module.exports = app;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment