Problems with CORS nodejs and Angular

2

I'm doing a webapp through the MEAN stack. The fact is that now I have developed a method to register objects in the database, but when I execute it, it is an error:

  

Response for preflight has invalid HTTP status code 404

I've been doing some research, and apparently it's in CORS. That is, the problem is in my server. I go to app.js in the part of the api, made with NodeJS, and this the code made:

app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control');
  res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, OPTIONS, DELETE');
  res.header('Allow', 'GET, PUT, POST, OPTIONS, DELETE');
  res.header('Content-Type', 'application/json; charset=utf-8'); 
  return next();
});

Can you know where the error exists? I have looked at more forms, but I do not give with it.

    
asked by H. Díaz 17.04.2017 в 14:47
source

1 answer

0

The problem was that when I ran the method I had created for the insertion of new records in the database, it was an error. Researching, I approached the solution apparently. Apparently, it was a problem related to the CORS (Cross-Origin Resource Sharing), resulting in the error that I specified in the question.

My method for collecting data was this (NodeJS):

var newProyect = req.body;
    proyect.create(newProyect, (err, proyect) => {
        if(err){
            res.status(500).send({message: 'Error al añadir el nuevo registro.'});
        }else{
            res.status(200).send({proyect: proyect});
        } 
    });

and my model this (Angular):

export class Proyect{
    constructor(
        public _id: string,
        public name: string,
        public subName: string,
        public description: string,
        public author: string,
        public date: string,
        public campo: string
    ){
    }
}

I realized that the property "_id" and "_v" are created automatically once they are registered in the database. And my model in NodeJs was this:

var mongoose = require('mongoose');
Schema = mongoose.Schema;

var proyectSchema = new Schema({
    name: String,
    subName: String,
    description: String,
    author: String,
    date: String,
    campo: String
});

module.exports = mongoose.model('proyect', proyectSchema);

What happened was that it sent a "project" object, created with the property "_id", although it was empty, it did not agree with the NodeJs model. With what it gave an error, an object with a property of more was created, and the server showed an internal error (500). Anyway, I needed the property "_id" to use it in one of the components of Angular, so I needed that property declared in the "Project" model, at least, with my knowledge, I came to the solution rethinking the collection method of data in the NodeJs controllers, leaving it like this:

function addProyect(req, res){
    var params = req.body;
    var proyect = new ProyectSchema();

    proyect.name = params.name;
    proyect.subName = params.subName;
    proyect.description = params.description;
    proyect.author = params.author;
    proyect.date = params.date;
    proyect.campo = params.campo;

    proyect.save((err, proyectStored) => {
        if(err){
            res.status(500).send({message : 'Error al guardar el nuevo regitro.'});
        }else{
            res.status(200).send({proyect : proyectStored});
        }
    });
} 

In this way, I collected only the data that I needed to enter a new record in the database. The field "_id" that still came empty emptied it, simply not using it. I hope this helps someone. As for giving me a problem with CORS, it was because of a badly named route, but everything is fixed. Maybe it's a bad way to develop code, but hey, I'm taking out the chestnuts. Greetings.

    
answered by 25.04.2017 в 14:35