首页 > 解决方案 > 某些路由在 ExpressJS 中不起作用

问题描述

我正在开发一个快速 API,但我的 R_Consultations.js 文件有问题

 const express = require('express');
var mysql = require('mysql');
const router = express.Router();

var con = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "",
    database: "cline_db"
  });

  router.get('/',function findAllConsultationsOfOnePatient(req, res) {
    //var sql='SELECT date_consultation, motif,Consultation.id_patient,id_medecin FROM Consultation join Patient on Patient.id_patient=Consultation.id_patient where Patient.cin_patient=';
    // Executing the MySQL query (select all data from the 'users' table).
     con.query('SELECT id_consultation,date_consultation, motif,Consultation.id_patient,Consultation.id_medecin FROM Consultation ', function (error, results, fields) {
       // If some error occurs, we throw an error.
       if (error) throw error;

       // Getting the 'response' from the database and sending it to our route. This is were the data is.
       res.send(results)
     });
   }
 );

  router.get('/:cin',function findAllConsultationsOfOnePatient(req, res) {
    //var sql='SELECT date_consultation, motif,Consultation.id_patient,id_medecin FROM Consultation join Patient on Patient.id_patient=Consultation.id_patient where Patient.cin_patient=';
    // Executing the MySQL query (select all data from the 'users' table).
     con.query('SELECT id_consultation,date_consultation, motif,Consultation.id_patient,Consultation.id_medecin FROM Consultation join Patient on Patient.id_patient=Consultation.id_patient where Patient.cin_patient=?',[req.params.cin], function (error, results, fields) {
       // If some error occurs, we throw an error.
       if (error) throw error;

       // Getting the 'response' from the database and sending it to our route. This is were the data is.
       res.send(results)
     });
   }
 );




router.get('/statistiques_annee',function getStatistiquesOfCetteAnnee(req, res) {

  // Executing the MySQL query (select all data from the 'users' table).
   con.query("SELECT count(id_consultation) as nombre_consultations FROM Consultation", function (error, results, fields) {
     // If some error occurs, we throw an error.
     if (error) throw error;

     // Getting the 'response' from the database and sending it to our route. This is were the data is.
     res.send(results)

   });

 }
);

//le nombre Total des consultaions d'un patient:

router.get('/statistiques/:cin',function findNumberOfAllConsultationsOfOnePatient(req, res) {
  //var sql='SELECT date_consultation, motif,Consultation.id_patient,id_medecin FROM Consultation join Patient on Patient.id_patient=Consultation.id_patient where Patient.cin_patient=';
  // Executing the MySQL query (select all data from the 'users' table).
   con.query('SELECT count(Consultation.id_consultation) as nombre_consultations FROM Consultation join Patient on Patient.id_patient=Consultation.id_patient where Patient.cin_patient=?',[req.params.cin], function (error, results, fields) {
     // If some error occurs, we throw an error.
     if (error) throw error;

     // Getting the 'response' from the database and sending it to our route. This is were the data is.
     res.send(results)
   });
 }
);



router.get('/statistiques_mois',function getStatistiquesOfCeMois(req, res) {
  //var sql='SELECT date_consultation, motif,Consultation.id_patient,id_medecin FROM Consultation join Patient on Patient.id_patient=Consultation.id_patient where Patient.cin_patient=';
  // Executing the MySQL query (select all data from the 'users' table).
   con.query('SELECT count(id_consultation) as nombre_consultations FROM Consultation where Month(date_consultation)=Month(curdate())', function (error, results, fields) {
     // If some error occurs, we throw an error.
     if (error) throw error;

     // Getting the 'response' from the database and sending it to our route. This is were the data is.
     res.send(results)
     console.log(results)
   });
 }
);




 module.exports = router;

当我运行“节点服务器”时

并调用“ http://localhost:5000/d_consultations/statistiques_mois ”,我有空反馈“[]”。

我所有的路线都有效,除了这条路线不起作用??

当我在文件“server.js”中对 con.use() 使用相同的 sql 请求时,它可以 100% 工作。

有什么问题 ?

标签: mysqlnode.jsapiexpress

解决方案


推荐阅读