我有快递服务,过了一会儿我得到: 错误:连接丢失:服务器关闭了连接。在Protocol.end
我的代码如下:
App.js
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.use(cookieParser());
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,UPDATE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
next();
});
const mysql = require('mysql');
const connection = mysql.createConnection({
host: '192.0.0.127',
port: 8080,
user: 'user',
password: '',
database: '',
insecureAuth: true
});
connection.connect(function (err) {
(err) ? console.log('Error en la conexion') : console.log('Conectado');
});
require('./routes/html-routes')(app, connection);
和html-routes.js:
const path = require("path");
const mysql = require("mysql");
const cookieParser = require("cookie-parser");
const express = require("express");
module.exports = function(app, connection) {
router.get("/disponible", function(req, res) {
var tipo = req.query.tipo;
connection.query("call sp_disponible('" + tipo + "');", function(
err,
data
) {
err ? res.send(err) : res.json({ disponible: data });
});
});
router.post("/distanciaTorton", function(req, res) {
var servicio = req.query.servicio;
connection.query(
"call sp_distanciaTorton('" + servicio + "');",
function(err, data) {
err ? res.send(err) : res.json({ distancia: data });
}
);
});
app.use("/tes/api", router);
};
我在Github上的存储库:https://github.com/GabrielaVillaAngeles/BackEndExpress
我的后端服务向我抛出以下日志:
Error: Connection lost: The server closed the connection.
at Protocol.end (/var/www/tescobedoBackEnd/node_modules/mysql/lib/protocol/Protocol.js:112:13)
at Socket. (/var/www/tescobedoBackEnd/node_modules/mysql/lib/Connection.js:97:28)
at Socket. (/var/www/tescobedoBackEnd/node_modules/mysql/lib/Connection.js:502:10)
at emitNone (events.js:111:20)
at Socket.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at args.(anonymous function) (/usr/lib/node_modules/pm2/node_modules/event-loop-inspector/index.js:138:29)
at _combinedTickCallback (internal/process/next_tick.js:139:11)
at process._tickCallback (internal/process/next_tick.js:181:9)
Error: Connection lost: The server closed the connection.
at Protocol.end (/var/www/tescobedoBackEnd/node_modules/mysql/lib/protocol/Protocol.js:112:13)
at Socket. (/var/www/tescobedoBackEnd/node_modules/mysql/lib/Connection.js:97:28)
at Socket. (/var/www/tescobedoBackEnd/node_modules/mysql/lib/Connection.js:502:10)
at emitNone (events.js:111:20)
at Socket.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at args.(anonymous function) (/usr/lib/node_modules/pm2/node_modules/event-loop-inspector/index.js:138:29)
at _combinedTickCallback (internal/process/next_tick.js:139:11)
at process._tickCallback (internal/process/next_tick.js:181:9)
Error: Connection lost: The server closed the connection.
at Protocol.end (/var/www/tescobedoBackEnd/node_modules/mysql/lib/protocol/Protocol.js:112:13)
at Socket. (/var/www/tescobedoBackEnd/node_modules/mysql/lib/Connection.js:97:28)
at Socket. (/var/www/tescobedoBackEnd/node_modules/mysql/lib/Connection.js:502:10)
at emitNone (events.js:111:20)
at Socket.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at args.(anonymous function) (/usr/lib/node_modules/pm2/node_modules/event-loop-inspector/index.js:138:29)
at _combinedTickCallback (internal/process/next_tick.js:139:11)
at process._tickCallback (internal/process/next_tick.js:181:9)
答案 0 :(得分:0)
您需要为每个数据库请求创建一个连接,或者使用一个连接池。使用游泳池可能是更好的选择。
将您的createConnection代码替换为以下内容:
config = {
connectionLimit: 15,
queueLimit: 10,
host: "...",
user: "...",
password: "..."
};
const db = mysql.createPool(config);
const connection = db.getConnection();
...
...
connection.query(..., callback(err, data) {...});