gabrielcsapo / node-git-server

🎡 A configurable git server written in Node.js
https://gabrielcsapo.github.io/node-git-server
MIT License
253 stars 73 forks source link

CORS Headers #53

Open simplygreatwork opened 5 years ago

simplygreatwork commented 5 years ago

I am working with isomorphic-git in the browser. I will be able to connect to node-git-server using a proxy server - but I'm also exploring adding cors headers upon node-git-server requests. But I don't see any way to set a new header without overriding handle() and handlers. Any thoughts on how to approach this?

simplygreatwork commented 5 years ago

So i'm just trying to wrap the handle method and set the CORS headers - not successful yet...

simplygreatwork commented 5 years ago

Making some progress.

npm install --save cors
const cors = require('cors')();
const handle = server.handle.bind(server);
server.handle = function(req, res) {
    cors(req, res, function() {
        handle(req, res);
    });
};
gabrielcsapo commented 5 years ago

I think the issue with overriding handle is that you are removing the functionality it had before, we can either expose the ability to introspect on each request, which could be valid for other use cases, or have you tried:

const cors = require('cors')();
const oldHandle = server.handle;
server.handle = function(req, res) {
    cors(req, res, function() {
        oldHandle(req, res);
    });
};