0

I have two route paths on my API on node.js and express, but this API crashed one time on 24 hours. I'm not sure what is exactly problem, but Can I get example how to fix that ?

My code:

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

For now he not crashed, and just stay on the line in terminal ECONNRESET, but the API not working after this message.

Pizhev Racing
  • 466
  • 1
  • 6
  • 23
  • you are using pm2 – aryan singh Jan 01 '20 at 12:56
  • Is your mysql instance up and running? If you cant find any problem with the mysql instance, try restarting the service running this script. – nima Jan 01 '20 at 12:58
  • aryan singh What exactly do you mean ? n1rna - I restart 500 times, not work for me ! My NPM version is 6.13.1 My NODE version is 12.13.1 – Pizhev Racing Jan 01 '20 at 13:11
  • @PizhevRacing I took your code and ran it and it worked fine. **ECONNRESET** indicates a TCP connection failure and it might be caused by other services on your computer, check this out it can help you troubleshoot it: https://stackoverflow.com/questions/59552671/node-js-and-express-restful-api-crashed-with-error-econnreset – MAS Jan 01 '20 at 15:59
  • This url contain my question, can you check your comment ? – Pizhev Racing Jan 01 '20 at 17:13
  • So... ok If my API crashed without any errors... How can I reset automaticly after he's crashed.. ? – Pizhev Racing Jan 01 '20 at 17:16
  • 1
    sorry, here's the right URL: https://stackoverflow.com/questions/17245881/node-js-econnreset – MAS Jan 01 '20 at 17:19
  • 1
    @PizhevRacing Basically you will need to run the app via a process manager. I'd recommend PM2: https://pm2.keymetrics.io/ – MAS Jan 01 '20 at 17:22
  • @PizhevRacing But also note that restarting the process might not be useful in your case because as I said earlier, the root issue is probably caused by other services which in turn needs to get troubleshot and resolved. – MAS Jan 01 '20 at 17:28
  • There have more than 5 answers. Who can I get for my code ? Can you give me example what to do ? – Pizhev Racing Jan 02 '20 at 05:53
  • mas - I run my code with pm2 monitor and pm2 list and this library say nothing.. The same error server > ECONNRESET, the API stay online but response not work. – Pizhev Racing Jan 03 '20 at 06:47

0 Answers0