Answers for "nodejs allow cors"

5

allow cross origin node

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});
Posted by: Guest on April-23-2020
26

express js cors

var express = require('express')
var cors = require('cors')  //use this
var app = express()

app.use(cors()) //and this

app.get('/user/:id', function (req, res, next) {
  res.json({user: 'CORS enabled'})
})

app.listen(5000, function () {
  console.log('CORS-enabled web server listening on port 5000')
})
Posted by: Guest on November-13-2020
1

allow cors express

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "YOUR-DOMAIN.TLD"); // update to match the domain you will make the request from
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});
Posted by: Guest on June-04-2020
2

nodejs CORS policy

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "YOUR-DOMAIN.TLD"); // update to match the domain you will make the request from
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

app.get('/', function(req, res, next) {
  // Handle the get for this route
});

app.post('/', function(req, res, next) {
 // Handle the post for this route
});
Posted by: Guest on April-22-2021
4

cors express

var allowedOrigins = ['http://localhost:3000',
                      'http://yourapp.com'];
app.use(cors({  
  origin: function(origin, callback){
    // allow requests with no origin     
    // (like mobile apps or curl requests)    
    if(!origin) 
      return callback(null, true);    
    if(allowedOrigins.indexOf(origin) === -1){
      var msg = 'The CORS policy for this site does not ' +                
          'allow access from the specified Origin.';      
      return callback(new Error(msg), false);    
    }    
    return callback(null, true);  
  }
}));
Posted by: Guest on August-05-2020
0

add express and cors to nodejs app

/*index.js*/                                                         
const express = require( 'express' );                                                             
const cors = require( 'cors' );
const app = express();                                                                                          const port = 3030;
//cors is enabled through out the entire app                                                                                  
app.use( cors() );
app.get( '/users', (request, response, next) => {                                                                                                                                                                                                                                                                            res.json( { info: 'cors is enabled' } )                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        });
app.listen( port, () => {                                                              
  console.log(`App is running on ${port}`)                                                      
  });
Posted by: Guest on August-03-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language