J. Doe J. Doe - 1 month ago 9
HTTP Question

Node connect add CORS header

I have this code.

var connect = require('connect');
var serveStatic = require('serve-static');
connect().use(serveStatic("public")).listen(80, function(){
});


I'd like to add cross origin policy headers to all responses.

How do I do this?

Answer

Here is the cors module that you can use.

var http = require('http');
var cors = require('cors');
var connect = require('connect');

var serveStatic = require('serve-static');

var app = connect();

app.use(cors());
app.use(serveStatic("public"));

http.createServer(app).listen(80);

By the way, is there a reason that you don't use express?