Angular + Node.JS CORS Issue Resources not loading

22 views
Skip to first unread message

Matías Huartamendía

unread,
Jan 11, 2025, 3:37:15 AMJan 11
to Angular and AngularJS discussion
I am currently writing Angular as front end and node.js as backend application.

Phones and other network PCs do not load the resources either all duue to CORS issue.

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3000/api/news. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 404

However I do have CORS in my code, any suggestions to try?

const express = require('express');
const app = express();
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const sqlite3 = require('sqlite3').verbose();
const path = require('path'); // Required for serving static files
const PORT = process.env.PORT || 3000;

const cors = require('cors');

app.use(cors({
    origin: '*', // Allow both local and network access
    methods: 'GET,POST,PUT,DELETE,OPTIONS',
    allowedHeaders: 'Content-Type,Authorization',
    credentials: true // Allow cookies if necessary
  }));
 
   
app.options('*', cors());

// Middleware for parsing JSON
app.use(express.json());  

// For serving static assets like images
app.use('/assets', express.static(path.join(__dirname, 'assets'), {
setHeaders: (res) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
  }
}));
Reply all
Reply to author
Forward
0 new messages