2

I want to disable CORS on localhost for a specific url, here is my code

    const express = require('express')
    const app = express()
    const port = 3000

    app.use(express.static('files'))

    app.all('/no-cors/', function(req, res, next) {
      res.header("Access-Control-Allow-Origin", "fake");
      next();
    });

    app.get('/', (req, res) => res.send('Hello World!'));

    app.get('/sample-json', (req, res) => res.json({"foo": "bar"}));

    // it shld show error in console
    app.get('/no-cors/sample-json', (req, res) => {
      res.json({"cors": "off"});
    });
    app

.listen(port, () => console.log(Example app listening on port 3000'))

but I open http://localhost:3000/no-cors/sample-json it still show me the json.

coure2011
  • 40,286
  • 83
  • 216
  • 349

1 Answers1

0

Path argument in express().get(path,...) is evaluated as whole string. Path in Express (and URL generally, not only in Express) does not work as folder structure.

That’s why the address /no-cors/sample-json is not catched with your app.all().

If you want it to work, try the path as /no-cors/*

Fide
  • 1,127
  • 8
  • 7