forked from yaph/nodejs-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·30 lines (26 loc) · 1.01 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/bin/env node
// OpenShift sample Node application
var http = require('http');
//Get the environment variables we need.
var ipaddr = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
http.createServer(function (req, res) {
var addr = "unknown";
var out = "";
if (req.headers.hasOwnProperty('x-forwarded-for')) {
addr = req.headers['x-forwarded-for'];
} else if (req.headers.hasOwnProperty('remote-addr')){
addr = req.headers['remote-addr'];
}
if (req.headers.hasOwnProperty('accept')) {
if (req.headers['accept'].toLowerCase() == "application/json") {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify({'ip': addr}, null, 4) + "\n");
return ;
}
}
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write("Welcome to Node.js on OpenShift!\n\n");
res.end("Your IP address seems to be " + addr + "\n");
}).listen(port, ipaddr);
console.log("Server running at http://" + ipaddr + ":" + port + "/");