0

Good morning, i'm asking for your help for my project. I want to display rainbow six stats since this api: https://www.npmjs.com/package/rainbowsix-api-node

I created a server node and express, a react's front. My problem: my node server crash stops 3 time out of 5, he is not stable and and I noticed that the r6 server npm solicited works well during off-peak hours. I run the server with npm start but when i update my react app the server often stops and displays this

after several searches, I do not understand where the problem may come from ; thanks for your help !

Console return this:

Server is listening on port 5000
undefined:1
<!DOCTYPE html>
^

SyntaxError: Unexpected token < in JSON at position 0

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `node index`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

my back's code:

const RainbowSixApi = require('rainbowsix-api-node');
const express = require('express');
const app = express();
const port = 5000;
const statsRouter = require('./routes/stats');
const R6 = new RainbowSixApi();

//let username = '<username here>';
let platform = 'ps4';

app.get('/api/stats/:username', (req, res) => {
  const username = req.params.username;

R6.stats(username, platform).then(response => {
    res.send(response);
  }).catch(error => {
    console.error(error)
  });
});


app.use('/api/stats', statsRouter)

app.listen(port, (err) => {
  if (err) {
    throw new Error('Erreur')
  }
  console.log(`Server is listening on port ${port}`);
});

My React Code

class Cards extends Component {

  constructor(props) {
    super(props);
    this.state = {
      statsArray: []
    };
  }

  componentDidMount(){
    const urls =
    [
      '/api/stats/username1',
      '/api/stats/username2',
      '/api/stats/username3',
      '/api/stats/username4',
      '/api/stats/username5',
    ]

    let promiseArray = urls.map(url => axios.get(url).then(response => response.data).catch(error => { console.error(error) }));

    Promise.all(promiseArray)
      .then(statsArray => this.setState({ statsArray }))
      }

1 Answer 1

1

This is not your problem. This is a problem of the 'rainbowsix-api-node' code.

Because in peak hours instead of returning the JSON value it is returning a HTML page(most probably an error page).

Saw the code in GitHub, this is where the error is generating.

request.get(endpoint, (error, response, body) => {
    if(!error && response.statusCode == '200') {
        return resolve(JSON.parse(body));
    } else {
        return reject(JSON.parse(body));
    }
})

Now, what you can do is just try..catch it.

app.get('/api/stats/:username', (req, res) => {
    const username = req.params.username;
    try{
        R6.stats(username, platform).then(response => {
            res.send(response);
        }).catch(error => {
            console.error(error)
        });
    }catch(error){
        console.error(error);
    };
});
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, thank you for that answer! I've the files of github, do you thin this problem can be fixed if i modifie the main.js file?
As I said you can add the try..catch block as I wrote above. it will fix your problem.

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.