Cross-Origin Request Blocked in Node and Angular

0

When I want to open my web application, the error appears in the console:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at link . (Reason: CORS header 'Access-Control-Allow-Origin' missing).

It works well in my other operating system (W7) that I have in dual boot, but in linux mint I do not see the queries. I use the same project folder, I leave the app.js file

import express from 'express'
import bodyParser from 'body-parser' // leer los datos que vengan con el request desde el frontend
import { question, auth } from './routes'

const app = express()

//leer la parte del request
  app.use(bodyParser.json())
  app.use(bodyParser.urlencoded({ extended: true }))
//

if (process.env.NODE_ENV === 'development') { // pedir que otro sitio pida recursos en modo de desarrollo
  app.use((req, res, next) => {
    res.setHeader('Access-Control-Allow-Origin', '*')
    res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Request-With, Content-Type, Accept')
    res.setHeader('Access-Control-Allow-Methods', 'POST, GET, PATCH, DELETE, OPTIONS')
    next()
  })
}

app.use('/api/questions', question)
app.use('/api/auth', auth)

export default app
    
asked by MarianoV 20.03.2018 в 07:17
source

1 answer

0

In my case using the operating system in windows 7, it happened that when the browser did not have the COORS plugin, it did not allow me to get backend information. When I added the COORS in the browser, when making requests from the Angular-cli application I obtained the information I needed, without needing to configure in the app.js or file where the server is configured to receive requests from my application with angular-cli .

Now, based on what I've seen, if a project with angular-cli and node is to be deployed on a server, it is not recommended to do it that way.

    
answered by 14.04.2018 в 19:02