I created Restful API with node.js & express and when I moved API and website to server machine When I trying to open the website I receive error in console:
Restful-API node.js & expressCross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource s cat. Statuode: (null).
// Create express app
var express = require("express")
var app = express()
var Pool = require('pg-pool')
var express = require("express")
var cors = require('cors')
var moment = require('moment');
app.use(cors());
app.options('*',cors());
// Server port
var HTTP_PORT = 3010
const pool = new Pool(
{
connectionLimit: 10,
host: '192.168.0.1',
user: 'usr',
port: '3333',
password: 'passwd',
database: 'dbName',
}
);
var floodguard_stations = '';
app.route('/stations')
.get(function (req, res) {
// omitted
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
//const id2 = req.query.id2;
pool.query(`SELECT * FROM "maritsa_public"."maritsa_codenums";`, function (error, result_stations, fields) {
if (error)
return res.status(500).json({ error: "Грешна заявка. Опитай отново !"})
floodguard_stations = result_stations;
res.json({ floodguard_stations })
});
});
The server it was uploaded to API have ssl. I call all URL links through the website with https
://192.168.0.1:3333/stationsCan I get guidance on how to stop requests from being blocked?
you have to pass * to specific domain in this line
from:
res.header("Access-Control-Allow-Origin", "*");
to:
res.header("Access-Control-Allow-Origin", "paste your same domain here");