Skip to content

Instantly share code, notes, and snippets.

@bathtimefish
Last active December 11, 2015 00:29
Show Gist options
  • Save bathtimefish/4516857 to your computer and use it in GitHub Desktop.
Save bathtimefish/4516857 to your computer and use it in GitHub Desktop.
node.js http serverで複数のremoteHost, requestMethodを判定してアクセス制御をする
http = require 'http'
allows =
"127.0.0.1" : ['POST','PUT']
"192.168.0.10" : ['GET','POST','PUT','DELETE']
httpServer = http.createServer (req, res)->
allowed = false
for ip, methods of allows
if ip == req.connection.remoteAddress
allowed = true
else
allowed = false
if allowed
unless methods.indexOf(req.method) == -1
allowed = true
break
else
allowed = false
if allowed
res.writeHead 200, {'Content-Type':'text/plain'}
res.end 'access allowed'
else
res.writeHead 403, {'Content-Type':'text/plain'}
res.end 'access forbidden'
httpServer.listen 3000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment