Skip to content

Commit

Permalink
Added a new server example that shows the use of Creationix's "Stack"…
Browse files Browse the repository at this point in the history
… library as the handler.
  • Loading branch information
TooTallNate committed Jan 5, 2011
1 parent e5f4886 commit be2c6ea
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions examples/stackServer.js
@@ -0,0 +1,37 @@
require('modbus-stack');
require('modbus-stack/server').createServer(
require('stack')(
// Handle "Read Coils" (well, we just call `next()`...)
functionCode(1, function(req, res, next) {
console.log("Got request for '1', but passing it on...");
next();
}),
// Handle "Read Input Registers"
functionCode(4, function(req, res, next) {
var resp = new Array(req.quantity);
for (var i=0; i < req.quantity; i++) {
resp[i] = req.startAddress + i;
}
res.writeResponse(resp);
}),
// If all else fails, call our "Illegal Function" response.
illegalFunction
)
).listen(502);


function functionCode(fc, callback) {
return function(req, res, next) {
if (req.functionCode === fc) {
callback.apply(this, arguments);
} else {
next();
}
}
}

function illegalFunction(request, response, err) {
var errCode = err && err.errno ? err.errno : 1;
console.log("responding with exception: " + errCode);
response.writeException(errCode);
}

0 comments on commit be2c6ea

Please sign in to comment.