patternjavascriptMinor
Creating a server for user Internet orders
Viewed 0 times
creatinguserinternetforserverorders
Problem
I've been learning the basics of node over the last couple of days and finally hooked up what I needed. I'm just after someone to comment on if I have gone the right way around this task.
It creates a server which users access to get updates to Orders through server side events.
The events are published through redis which I subscribe to the queue.
```
var redis = require('redis'),
http = require('http'),
os = require("os"),
PHPUnserialize = require('php-unserialize');
function sendServerSendEventForInternetOrders(req, res) {
req.socket.setTimeout(0); // let request last as long as possible
var hostname;
if(os.hostname() == "kipos"){
hostname = os.hostname()+".dev";
}else{
hostname = os.hostname();
}
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Access-Control-Allow-Origin': 'http://'+hostname
});
res.write('\n');
var phpSession = req.url.match('\/internetOrders\/(.*)')[1];
console.log(phpSession + " - created client");
var redisClient = redis.createClient();
// In case we encounter an error...print it out to the console
redisClient.on("error", function (err) {
console.log(phpSession + " - Redis Error: " + err);
});
var phpSession = req.url.match('\/internetOrders\/(.*)')[1];
var taID;
var takeaway;
redisClient.get("PHPREDIS_SESSION:" + phpSession, function (err, reply) {
if (reply != null || err != null) {
taID = PHPUnserialize.unserializeSession(reply).LoggedIn.TaID;
console.log(phpSession + " - " + taID);
startSub();
} else {
res.end();
console.log(phpSession + " - exit");
}
});
function startSub() {
redisClient.select(1, function () {
redisClient.get(taID + "T", function (err, reply) {
if (reply == null) {
It creates a server which users access to get updates to Orders through server side events.
The events are published through redis which I subscribe to the queue.
```
var redis = require('redis'),
http = require('http'),
os = require("os"),
PHPUnserialize = require('php-unserialize');
function sendServerSendEventForInternetOrders(req, res) {
req.socket.setTimeout(0); // let request last as long as possible
var hostname;
if(os.hostname() == "kipos"){
hostname = os.hostname()+".dev";
}else{
hostname = os.hostname();
}
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Access-Control-Allow-Origin': 'http://'+hostname
});
res.write('\n');
var phpSession = req.url.match('\/internetOrders\/(.*)')[1];
console.log(phpSession + " - created client");
var redisClient = redis.createClient();
// In case we encounter an error...print it out to the console
redisClient.on("error", function (err) {
console.log(phpSession + " - Redis Error: " + err);
});
var phpSession = req.url.match('\/internetOrders\/(.*)')[1];
var taID;
var takeaway;
redisClient.get("PHPREDIS_SESSION:" + phpSession, function (err, reply) {
if (reply != null || err != null) {
taID = PHPUnserialize.unserializeSession(reply).LoggedIn.TaID;
console.log(phpSession + " - " + taID);
startSub();
} else {
res.end();
console.log(phpSession + " - exit");
}
});
function startSub() {
redisClient.select(1, function () {
redisClient.get(taID + "T", function (err, reply) {
if (reply == null) {
Solution
One thing that you could change is the way that this if else statement is written
Here you are deciding what a variables value should be using an if/else statement, this is a good spot to use a JavaScript ternary operator.
Example Ternary Statement:
so your code would look like this
var hostname;
if(os.hostname() == "kipos"){
hostname = os.hostname()+".dev";
}else{
hostname = os.hostname();
}Here you are deciding what a variables value should be using an if/else statement, this is a good spot to use a JavaScript ternary operator.
Example Ternary Statement:
var variableName = red == blue ? trueValueAssignment : falseValueAssignment;so your code would look like this
var hostname = os.hostname() == "kipos" ? os.hostname() + ".dev" : os.hostname();Code Snippets
var hostname;
if(os.hostname() == "kipos"){
hostname = os.hostname()+".dev";
}else{
hostname = os.hostname();
}var variableName = red == blue ? trueValueAssignment : falseValueAssignment;var hostname = os.hostname() == "kipos" ? os.hostname() + ".dev" : os.hostname();Context
StackExchange Code Review Q#61027, answer score: 3
Revisions (0)
No revisions yet.