I just started my nodejs express template buy cors is not working.
I used npm install cors --save
here is the file:
var express = require('express'); var cors = require('cors'); var app = express(); app.use(cors()); var corsOptions = { origin: 'https://example.com/', optionsSuccessStatus: 200 }; app.get('/', cors(corsOptions), function(req, res, next) { res.json({ message: 'hooray! welcome to our api!' }); }); app.get('/tt', function(req, res, next) { res.json({ message: 'hooray! welcome to our api!' }); }); var port = process.env.PORT || 3030; app.listen(port); console.log('Magic happens on port ' + port); Now with the above code when I access localhost:3030/tt or / I still see the content and I shouldn't
What's wrong with this.. I just lost like 2 hours working on this.. :( At this time I would like not to use CORS, but in near future when my app is finished, I want to allow incoming calls only from my project, since this app will be my API.