【问题标题】:Crash server node.js崩溃服务器 node.js
【发布时间】:2018-12-14 01:32:25
【问题描述】:

早上好, 我正在为我的项目寻求您的帮助。我想显示自此 api 以来的彩虹六号统计数据:https://www.npmjs.com/package/rainbowsix-api-node

我创建了一个服务器节点并表达了一个反应的前端。我的问题:我的节点服务器崩溃在 5 次中有 3 次停止,他不稳定,我注意到 npm 请求的 r6 服务器在非高峰时段运行良好。 我用 npm start 运行服务器,但是当我更新我的 react 应用程序时,服务器经常停止并显示这个

经过多次搜索,我不明白问题可能来自哪里;感谢您的帮助 !

控制台返回:

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! back@1.0.0 start: `node index`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the back@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

我的背号:

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}`);
});

我的反应代码

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 }))
      }

【问题讨论】:

    标签: node.js reactjs express npm axios


    【解决方案1】:

    这不是你的问题。这是'rainbowsix-api-node' 代码的问题。

    因为在高峰时段,它不是返回 JSON 值,而是返回 HTML 页面(很可能是错误页面)。

    在 GitHub 中看到代码,这是产生错误的地方。

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

    现在,你能做的就是try..catch它。

    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);
        };
    });
    

    【讨论】:

    • 好的,谢谢你的回答!我有github的文件,请问修改main.js文件能解决这个问题吗?
    • 正如我所说,您可以添加try..catch 块,就像我上面写的那样。它会解决你的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    相关资源
    最近更新 更多