node.js和表达宁静的API崩溃,并出现错误:ECONNRESET |

时间:2020-01-01 12:50:45

标签: javascript node.js rest api express

我在node.js上的API上有两条路由路径并进行了表达,但是该API在24小时内崩溃了一次。 我不确定到底是什么问题,但是可以举个例子解决这个问题吗?

我的代码:

// Create express app
var express = require("express")
var app = express()
var mysql = require('mysql')
var express = require("express")
var cors = require('cors')

app.use(cors())

// Server port
var HTTP_PORT = 8000

var con = mysql.createConnection({
  host: "192.168.0.1",
  port: "1234",
  user: "username",
  password: "pass"
});

var aladinModel = '';
var aladinModelStations = '';

function formatDate(date) {
  var d = new Date(date),
      month = '' + (d.getMonth() + 1),
      day = '' + d.getDate(),
      year = d.getFullYear();

  if (month.length < 2) 
      month = '0' + month;
  if (day.length < 2) 
      day = '0' + day;

  return [year, month, day].join('-');
}

var dateNow = formatDate(Date());

app.route('/')
  .get(function (req, res) {
    // omitted
    res.setHeader('Access-Control-Allow-Origin', '*', 'Cache-Control', 'private, no-cache, no-store, must-revalidate');
    //const date = req.query.date;
    const id = req.query.id;
    const daysForward = req.query.daysForward;

      const query = `CALL Get_mod_cell_values_meteogram_cell('${dateNow}', ${id}, ${daysForward})`;
      con.query(query, function (err, result, fields) {
        if (err) 
        return res.status(500).json({ error: "Грешна заявка. Опитай отново !"})
        aladinModel = result;
        res.json({ aladinModel })
      });
  });

app.route('/stations')
  .get(function (req, res) {
    // omitted
    res.setHeader('Access-Control-Allow-Origin', '*', 'Cache-Control', 'private, no-cache, no-store, must-revalidate');
    const id2 = req.query.id2;
      const query2 = `SELECT Station,Ime FROM stations_cells WHERE Station=${id2}`;
      con.query(query2, function (err, result2, fields) {
        if (err) 
        return res.status(500).json({ error: "Грешна заявка. Опитай отново !"})
        aladinModelStations = result2;
        res.json({ aladinModelStations })
      })
  });

  // Start server
app.listen(HTTP_PORT, () => {
  console.log("Server running on port %PORT%".replace("%PORT%", HTTP_PORT))
});

con.on('error', function(err) {
  console.log(err.code); // 'ER_BAD_DB_ERROR'
});

app.use(function (req, res) {
  res.status(404);
});

目前他还没有崩溃,只是停留在终端ECONNRESET的线路上,但是此消息后API无法正常工作。

0 个答案:

没有答案