Node.js CORS Middleware

2.8.6 · active · verified Sat Apr 18

CORS is a Node.js middleware for Express and Connect that simplifies setting Cross-Origin Resource Sharing (CORS) response headers. It helps browsers determine which origins can read responses from your server. The current stable version is 2.8.6. Releases are made periodically to address maintenance and update documentation.

Common errors

Warnings

Install

Imports

Quickstart

This example shows how to enable CORS for all routes in an Express application, adding the `Access-Control-Allow-Origin: *` header to all responses.

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

// Enable all CORS requests for all routes
app.use(cors());

app.get('/products/:id', function (req, res, next) {
  res.json({msg: 'Hello'});
});

app.listen(80, function () {
  console.log('web server listening on port 80');
});

view raw JSON →