开发者社区> 问答> 正文

带有express和node.js的RestFul应用程序接口在每次修复时都会崩溃?

每次我的应用程序接口在24小时内崩溃一次两次。

我看到这个错误:

ECONNRESET
C:\Users\Admin\Desktop\node-express 1.0.1\server.js:50
        if (err) throw err;
                 ^

Error: Cannot enqueue Query after fatal error.
    at Protocol._validateEnqueue (C:\Users\Admin\Desktop\node-express 1.0.1\node_modules\mysql\lib\protocol\Protocol.js:212:16)
    at Protocol._enqueue (C:\Users\Admin\Desktop\node-express 1.0.1\node_modules\mysql\lib\protocol\Protocol.js:138:13)
    at Connection.query (C:\Users\Admin\Desktop\node-express 1.0.1\node_modules\mysql\lib\Connection.js:201:25)
    at C:\Users\Admin\Desktop\node-express 1.0.1\server.js:49:11
    at Layer.handle [as handle_request] (C:\Users\Admin\Desktop\node-express 1.0.1\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\Admin\Desktop\node-express 1.0.1\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\Admin\Desktop\node-express 1.0.1\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\Admin\Desktop\node-express 1.0.1\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\Admin\Desktop\node-express 1.0.1\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\Admin\Desktop\node-express 1.0.1\node_modules\express\lib\router\index.js:335:12) {
  code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR',
  fatal: false
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! express-api@1.0.0 start: `node server.js`
npm ERR!
npm ERR! Failed at the express-api@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Admin\AppData\Roaming\npm-cache\_logs\2019-12-31T06_50_16_476Z-debug.log
PS C:\Users\Admin\Desktop\node-express 1.0.1> npm start

> express-api@1.0.0 start C:\Users\Admin\Desktop\node-express 1.0.1
> node server.js

我不知道如果这个状态出现在第50行会发生什么

if (err) throw err;
        aladinModel = result;
        res.json({ aladinModel })
      });
    } catch (error) { 
      console.log("Error query database!!!");
    }

我所有的代码

// 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 aladin_surfex.Get_mod_cell_values_meteogram_cell('${dateNow}', ${id}, ${daysForward})`;
      con.query(query, function (err, result, fields) {
        if (err) 
        return res.status(500).json({ error: "Internal server 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 aladin_surfex.stations_cells WHERE Station=${id2}`;
      con.query(query2, function (err, result2, fields) {
        if (err) 
        return res.status(500).json({ error: "Internal server error"})
        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);
});

当这个人从第一条评论开始写我的时候,我更新了代码。其他地方会有错误吗?我去掉了航线上的试捕区。

我想我这样做是为了没有可能突破这个网站

展开
收起
sossssss 2019-12-31 20:48:48 1743 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
探究 Node.js 的服务端之路 立即下载
个推微服务实践 基于OpenResty 和Node.js 立即下载
沪江基于Node.js大规模应用实践 立即下载