Home res.redirect() cors not working in MEAN app
Reply: 0

res.redirect() cors not working in MEAN app

user2344
1#
user2344 Published in May 20, 2018, 12:02 pm

I have a MEAN APP with Angular 5 and I am trying to redirect a user to a external URL but I don't know why i get the next two errors in console

  • Failed to load https://www.google.com.ar/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

  • Http failure response for (unknown url): 0 Unknown Error

I already enabled CORS but still doesn't work

Component Angular 5

this.http.get('/api/producto').subscribe(data => {
  console.log(data);
});

APP.JS

var express = require('express');
var compression = require('compression');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var bodyParser = require('body-parser');
var morgan = require('morgan');
var mongoose = require('mongoose');
var passport = require('passport');
var config = require('./config/database');
var cors = require('cors');

var app = express();
app.use(cors());

app.get('/api/producto', cors(), function (req, res) {
  res.status(301).redirect("https://www.google.com.ar")
  res.end();
})

app.use(compression());

mongoose.Promise = require('bluebird');
mongoose.connect(config.database, { promiseLibrary: require('bluebird') })
  .then(() => console.log('connection succesful'))
  .catch((err) => console.error(err));

app.use(passport.initialize());

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ 'extended': 'false' }));
app.use(express.static(path.join(__dirname, 'dist')));

app.use('', express.static(path.join(__dirname, 'dist')));
app.use('/', express.static(path.join(__dirname, 'dist')));

app.use(function (req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

app.use(function (err, req, res, next) {
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

If I go to "http://localhost:3000/api/producto" the redirecting works perfectly but if the component calls the api it doesn't work

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.340241 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO