Skip to main content

Command Palette

Search for a command to run...

How can you allow CORS in Express.js?

Updated
1 min read
How can you allow CORS in Express.js?
R

Full Stack Developer with a passion for building web applications. PHP, Node.js, Laravel, MySQL, MongoDB. Love collaborating & making a difference

CORS is a node.js package for providing a Connect/Express middleware that can be used to enable CORS with various options.

$ npm install cors
var express = require('express')
var cors = require('cors')
var app = express()

app.use(cors())

app.get('/products/:id', function (req, res, next) {
  res.json({msg: 'This is CORS-enabled for all origins!'})
})

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

Sources:

Express Cors Middleware. expressjs.com/en/resources/middleware/cors.html.

More from this blog

R

Raja Muhammad Asher - Senior Software Engineer - Full Stack Developer

157 posts

Full Stack Developer with a passion for building web applications. PHP, Node.js, Laravel, ExpressJS, MySQL, MongoDB. Love collaborating & making a difference