U
U
urajo2019-09-15 01:24:00
JavaScript
urajo, 2019-09-15 01:24:00

How to implement two connections to the database?

The situation is this, to do two connection.end - it's impossible, to leave the connection too (for some reason, if I don't close the connection - the whole server crashes after 30 seconds Error: Connection lost: The server closed the connection.), what to do?

const express = require('express'),
  app = express(),
  mysql = require('mysql'),
  connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'pcnumber'
  }),
  port = 3000,
  path = require('path'),
  bodyParser = require('body-parser');
let searchfree = '',
  admins = [],
  resultInp = [];

app.set('views', path.join(__dirname, '/pages'));
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));

connection.connect((err) => {
  if(err){
    return console.log('Ошибка соединения с базой');
  }
  
  console.log('Подключение к базе успешно');
});

connection.query('SELECT name FROM admin',(error, result, fields) => {
  for(var i = 0; i < result.length; i++) {
    admins.push(result[i].name);
  }
  connection.end();
});

app.get('/pcnumber',(request, response) => {
  response.render('pcnumber',{
    arr: admins
  });
});

app.get('/result',(request, response) => {
  resultInp.push(request.query.info, request.query.admin);
  connection.query("SELECT * FROM `pcnumber-one` WHERE status LIKE 'free'",(error, result, fileds) => {
    if(result){
      console.log('Есть свободный');
    }
    connection.end();
  });
});

app.listen(port,(err) => {
  if(err){
    return console.log('Ошибка при запуске сервера');
  }
  
  console.log(`Сервер запущен, порт ${port}`);
});

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question