I would like to protect my API with CORS. I want my API to have call access only from the selected domain.
I am using node.js and express, so I add to my project: https://github.com/expressjs/cors
And example code:
var express = require('express')
var cors = require('cors')
var app = express()
var corsOptions = {
origin: 'http://example.com',
optionsSuccessStatus: 200
}
app.get('/test', cors(corsOptions), function (req, res, next) {
res.json({msg: 'This is CORS-enabled for only example.com.'})
})
But if I make a request via POSTMAN from localhost, I still get a response from this routing. This should be blocked and only work for requests from one domain (example.com).
I tried before:
router.get('/test', cors(corsOptions), function(req, res, next) {
console.log(corsOptions);
res.header("Access-Control-Allow-Origin", "http://www.example.com);
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.status(200).send({ work: true });
});
But this also lets my requests from POSTMAN...
I would like to have an API that can be referenced only if it comes to my example.com page and from there are requests sent by JavaScript.