Last active
August 29, 2015 14:15
-
-
Save jcdalton2201/6dc97daaf1616f9d3c3e to your computer and use it in GitHub Desktop.
NodeServer
This file contains hidden or 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
| #!/bin/env node | |
| var restify = require('restify'); | |
| var port = process.env.NODE_PORT || 8081; | |
| var ip = '127.0.0.1'; | |
| //create servers | |
| var server = restify.createServer(); | |
| //create the handlers. | |
| server.use(restify.CORS()); | |
| server.use(restify.bodyParser({ mapParams: false })); | |
| server.use(restify.queryParser({ mapParams: false })); | |
| server.use(restify.gzipResponse()); | |
| //Handle if we don't know the method | |
| function unknownMethodHandler(req, res) { | |
| console.log(req.method); | |
| if (req.method.toLowerCase() === 'options' ) { | |
| var allowHeaders = ['Accept', 'Accept-Version', 'Content-Type', 'Api-Version', 'Origin', 'X-Requested-With']; | |
| if (res.methods.indexOf('OPTIONS') === -1) { | |
| res.methods.push('OPTIONS'); | |
| } | |
| res.header('Access-Control-Allow-Credentials', false); | |
| res.header('Access-Control-Allow-Headers', allowHeaders); | |
| res.header('Access-Control-Allow-Methods', 'POST,GET,PUT,DELETE,OPTIONS'); | |
| res.header('Access-Control-Allow-Origin', '*'); | |
| return res.send(200); | |
| } | |
| else | |
| { | |
| return res.send(new restify.MethodNotAllowedError()); | |
| } | |
| } | |
| server.on('MethodNotAllowed', unknownMethodHandler); | |
| server.listen(port,ip,function(){ | |
| console.log('%s listen at %s',server.name,server.url); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment