Home » Featured, Headline, Software Development

Node.js – How to enable Cross Origin Resource Sharing (CORS)

13 April 2017 No Comment

Enabling CORS on a Node.js server can be done using the code snippet below.
This code uses express as the framework for node development.

var express = require('express');

var app = express();

app.use(function(req, res, next) {
  // add the access control headers
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

app.get('/', function (req, res) {
  var data = {};

app.get('/download', function(req, res){
  var file = __dirname + '/download.tar';
  res.download(file); // Set disposition and send it.

Hope this is helpful!

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.

To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image