1

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
7
  • And yes javascript is enabled in my chrome browser, I checked. I am getting the same issue in other browsers as well. Commented Mar 28, 2021 at 7:13
  • 2
    I don't know who's answering these requests, but I have a strong feeling that it ain't your express server. Commented Mar 28, 2021 at 7:23
  • That message usually shows up in web pages that have <noscript> tags. I'm not sure how it would appear in an API response. Commented Mar 28, 2021 at 7:34
  • API response is returning HTML instead of json Commented Mar 28, 2021 at 7:50
  • 1
    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). Commented Mar 28, 2021 at 8:02

2 Answers 2

1

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

axios.get('/api', {
  auth,
  headers,
  proxy
}
Sign up to request clarification or add additional context in comments.

Comments

1
  • 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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.