Skip to content

Instantly share code, notes, and snippets.

@lohmann
Forked from jcdalton2201/index.js
Last active August 28, 2015 01:43
Show Gist options
  • Select an option

  • Save lohmann/dae934612f745164a38b to your computer and use it in GitHub Desktop.

Select an option

Save lohmann/dae934612f745164a38b to your computer and use it in GitHub Desktop.

Revisions

  1. @jcdalton2201 jcdalton2201 renamed this gist Feb 10, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @jcdalton2201 jcdalton2201 created this gist Feb 10, 2015.
    39 changes: 39 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    #!/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);
    });