J. Doe
J. Doe

Reputation: 13

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?

Upvotes: 1

Views: 1085

Answers (2)

Stavros Zavrakas
Stavros Zavrakas

Reputation: 3063

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?

Upvotes: 0

WitVault
WitVault

Reputation: 24140

You can use Cors npm package to enable cors support.

Installation:

$ npm install cors

Usage:

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

app.use(cors());

Upvotes: 2

Related Questions