I Get An Error Trying To Connect My Backend With

- 1 answer

I get the next error when I try to connect my server with I'm making an app with angular and node and this is what I get:

Solicitud de orígenes cruzados bloqueados: La misma política de origen deshabilita la lectura remota del elemento en ‘http://localhost:3000/’. (Razón: La credencial no está soportada si el encabezado CORS ‘Access-Control-Permitir-Origen' está ‘*’).

URL pedida:http://localhost:3000/
Método de la solicitud:GET
Dirección remota:
Código del estado:404

// Requires and const definition
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const cors = require('cors');
const mongoose = require('mongoose');
const mongoURI = "mongodb://localhost:27017/proyecto_daw";
const http = require('http');
const normalizePort = require('normalize-port');

// Configuración CORS
app.use((req, res, next) => {
   res.header('Access-Control-Allow-Origin', '*');
   res.header('Access-Control-Allow-Headers', 'Authorization, X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Allow-Request-Method');
   res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE');
   res.header('Allow', 'GET, POST, OPTIONS, PUT, DELETE');

// Middlewares
app.use(bodyParser.urlencoded({ extended: false }));

const port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

// Routes
const user_routes = require('./routes/user.routes');
const follow_routes = require('./routes/follow.routes');
const group_routes = require('./routes/groups.routes');
const publication_routes = require('./routes/publication.route');
app.use('/api', user_routes);
app.use('/api', follow_routes);
app.use('/api', group_routes);
app.use('/api', publication_routes);

var server = http.createServer(app);
var io = require('').listen(server);


 console.log('new connection made.');

 socket.on('join', function(data){

   console.log(data.user + 'joined the room : ' +;'new user joined', {user:data.user, message:'has joined this room.'});

 socket.on('leave', function(data){

   console.log(data.user + 'left the room : ' +;'left room', {user:data.user, message:'has left this room.'});


 socket.on('message',function(data){'new message', {user:data.user, message:data.message});

// Database
mongoose.Promise = global.Promise;
mongoose.set('useFindAndModify', false);
mongoose.connect(mongoURI, { useNewUrlParser: true })
 .then(() => {
   console.log("BD conectada");

   // Creación del servidor
   app.listen(app.get('port'), () => {
     console.log("Puerto " + app.get('port'));
 .catch(err => console.log(err));



require('') returns a function that constructs the socket object which you should provide the server to.

Thus that's what you should do:

var socketIO = require('');
var theSocketObject = socketIO(server);

And then use the theSocketObject to emit/listen to events.