Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// parse body json params
app.use(restify.plugins.bodyParser({ mapParams: true }));
// setup CF port
var port = Number(process.env.VCAP_APP_PORT || 8080);
app.listen(port, function () {
//write a message using the logMessage method of the logger.
log.logMessage("info", "listening on port: %d", port);
});
// create a json object and send it as custom log with the given logger.
var stats = {};
stats.node_version = process.version;
log.logMessage("info", "Runtime statistics", stats);
// handling post messages
app.post("/post_message", function (req, res, next) {
var msg = {
"name": req.body.name,
"time": req.body.time,
"message": req.body.message,
"timestamp": (new Date()).getTime()
};
req.logger.info(`received message from '${msg.name}': ${msg.message}`);
if (redisRunning) {
pub.publish("message", JSON.stringify(msg));
} else {
pushLocal(msg);
// inserts the logger in the server network queue, so each time a https request is recieved, it is will get logged.
app.use(log.logNetwork);
app.use("/", express.static(__dirname + "/public"));
// setup CF port
var port = Number(process.env.VCAP_APP_PORT || 8080);
app.listen(port, function () {
//write a message using the logMessage method of the logger.
log.logMessage("info", "listening on port: %d", port);
});
// create a json object and send it as custom log with the given logger.
var stats = {};
stats.node_version = process.version;
log.logMessage("info", "Runtime statistics", stats);
// handling post messages
app.post("/post_message", function (req, res) {
var msg = {
"name": req.body.name,
"time": req.body.time,
"message": req.body.message,
"timestamp": (new Date()).getTime()
};
req.logger.info(`received message from '${msg.name}': ${msg.message}`);
if (redisRunning) {
pub.publish("message", JSON.stringify(msg));
} else {
pushLocal(msg);
function redisEndHandler(err) {
if (redisRunning) {
log.logMessage("info", "redis disconnected!");
}
redisRunning = false;
}
function synchronize() {
if (syncPointer != -1) {
log.logMessage("verbose", "redis synchronization!");
var syncMessage = {};
syncMessage.data = messages.splice(syncPointer, messages.length - syncPointer);
setTimeout(function () {
pub.publish("message", JSON.stringify(syncMessage));
}, 200);
syncPointer = -1;
}
}
function redisErrorHandler(err) {
if (redisRunning) {
log.logMessage("error", "redis error occured!");
}
redisRunning = false;
}
function synchronize() {
if (syncPointer != -1) {
log.logMessage("verbose", "redis synchronization!");
var syncMessage = {};
syncMessage.data = messages.splice(syncPointer, messages.length - syncPointer);
setTimeout(function () {
pub.publish("message", JSON.stringify(syncMessage));
}, 200);
syncPointer = -1;
}
}
app.listen(port, function () {
//write a message using the logMessage method of the logger.
log.logMessage("info", "listening on port: %d", port);
});
function synchronize() {
if (syncPointer != -1) {
log.logMessage("verbose", "redis synchronization!");
var syncMessage = {};
syncMessage.data = messages.splice(syncPointer, messages.length - syncPointer);
setTimeout(function () {
pub.publish("message", JSON.stringify(syncMessage));
}, 200);
syncPointer = -1;
}
}
function redisEndHandler() {
if (redisRunning) {
log.logMessage("info", "redis disconnected!");
}
redisRunning = false;
}
function redisConnectionHandler() {
if (!redisRunning) {
log.logMessage("info", "redis connected!");
}
redisRunning = true;
sub.subscribe("message", "sync");
synchronize();
}