Created
August 28, 2014 03:13
-
-
Save januswel/d3cc0db2f449af4336b4 to your computer and use it in GitHub Desktop.
Node.js サーバーを 80 番ポートで動かす ref: http://qiita.com/janus_wel/items/ee0ea612a075f197a219
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"user": "vagrant", | |
"port": 80 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var PORT = 80; | |
var HOSTNAME = '127.0.0.1'; | |
var USER = 'nodejs'; | |
http.createServer(function (req, res) { | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
res.end('Hello World\n'); | |
}).listen(PORT, HOSTNAME, function () { | |
console.log('change user'); | |
process.setuid(USER); | |
}); | |
console.log('Server running at http://' + HOSTNAME + ':' + PORT + '/'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var WELL_KNOWN_PORT = 1024; | |
var config = require('config'); | |
var port = config.port || 80; | |
var user = config.user || 'nodejs'; | |
var os = require('os'); | |
var hostname = os.hostname() || '127.0.0.1'; | |
var http = require('http'); | |
http.createServer(function (req, res) { | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
res.end('Hello World\n'); | |
}).listen(port, hostname, function () { | |
if (port < WELL_KNOWN_PORT) { | |
console.log('Attempting setuid to user "' + user + '"...'); | |
try { | |
process.setuid(user); | |
console.log('Succeeded to setuid'); | |
} catch (err) { | |
console.log('Failed to setuid'); | |
process.exit(1); | |
} | |
} | |
}); | |
console.log('Server running at http://' + hostname + ':' + port + '/'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment