0
votes

express.js

const app = express();
app.use(express.static(path.join(__dirname, 'build')));
app.use(express.static(path.join(__dirname, 'doc')));
app.use(express.json({ limit: "10mb", type: "application/json" }));
app.use(cors())

app.use('/search', proxy({
    pathRewrite: {
        '^/search': '/search'
    },
    target:<target API URL>,
    secure: false
}));

const httpServer = http.createServer(app);
httpServer.listen(HTTP_PORT);

Axios call

  getBacklogItemsData = (query) => {
    const jqlQuery = this.getBacklogItemsJQLQuery(query);
    const headers = {
      'Content-Type': 'application/json',
      'Authorization': "Basic <authString>",
      'Access-Control-Allow-Origin': '*',
    };
    const auth = {
       username: <id>,
       password: <pass>,
    }
    const proxy = {
      host: 'localhost',
      port: 5000
    };
    let config = {
      headers
    }
    return axios.get(`/search?jql=${jqlQuery}/`, auth, headers, proxy, config);
  }

package.json

  "proxy": "https://localhost:5000",
  "homepage": ".",

Error - You need to enable JS

Requested headers - Req header

What have I tried so far?

  1. Direct call to the API with the same requested parameters returns the correct response (using postman and web browser)
  2. Added "." as homepage in package.json
  3. Added localhost:5000 as proxy in package.json
  4. Used corsOption -
var corsOptions = {
    origin: 'http://localhost:5000',
    optionsSuccessStatus: 200
  }

app.use('/search', cors(corsOptions), proxy({
    pathRewrite: {
        '^/search': '/search'
    },
    target: baseURL,
    secure: false
}), function (req, res, next) {
    res.json({msg: 'This is CORS-enabled for only example.com.'})
  });
  1. I did a direct axios request to target API and got CORS issue
axios.get(`<TargetAPIUrl>/search?jql=${jqlQuery}/`, auth, headers, proxy, config);
  1. Added proxy parameters to axios.get call as shown above
2
And yes javascript is enabled in my chrome browser, I checked. I am getting the same issue in other browsers as well.Mansi
I don't know who's answering these requests, but I have a strong feeling that it ain't your express server.Thomas
That message usually shows up in web pages that have <noscript> tags. I'm not sure how it would appear in an API response.Barmar
API response is returning HTML instead of jsonMansi
A server running in nodejs answering with "please turn on JS"? That would be silly, like "knock, knock ... I'm not here". But that was just intuition. I just realized (as you answered to Barmar) that this is the rendered response, not necessarily the literal text in the response body. And that this may be the default page returned when no route matches the request (for whatever reason).Thomas

2 Answers

1
votes

It seems like the axios.get should be called like this.

axios.get('/api', {
  auth,
  headers,
  proxy
}
1
votes
  • Acredito que você precisa nomear os parâmetros ao passar para o método:
const options = {
    headers = {
      'Content-Type': 'application/json',
      'Authorization': "Basic <authString>",
      'Access-Control-Allow-Origin': '*',
    },
    auth = {
       username: <id>,
       password: <pass>,
    },
    proxy = {
      host: 'localhost',
      port: 5000
    }
 }

axios.get(`<TargetAPIUrl>/search?jql=${jqlQuery}/`, options);