某些行的javascript / node.js代码未执行

时间:2019-07-15 15:01:49

标签: javascript node.js express

app.get未执行,为什么?我也尝试过放app.listen(3000)。我想获取我通过第一个网页传递的参数。这段代码希望获取另一个网页传递的参数,然后构建mysql查询和表。在此先感谢大家。如果您需要一些其他详细信息,请问我!

const http = require('http');
const mysql = require('mysql');

const pool = mysql.createPool({
  host: '127.0.0.1',
  user: 'andrea',
  password: 'password',
  database: 'scuola',
  charset: 'utf8'
});

//html string that will be send to browser
var reo ='<html><head><title>Voti</title></head><body><h1>Voti</h1>{${table}}</body></html>';

var express = require("express");
var app = express();

let idAlunno=0;

var sqlAlunni ='SELECT * FROM registro_voti WHERE id_alunno =';

idAlunno = '"'+idAlunno+'"';
//console.log("idAlunno="+idAlunno);

function setResHtml(sql, cb){
      pool.getConnection((err, con)=>{
        if(err) throw err;

        app.get('*',function(request, response){
            console.log("app.get()");
            console.log("request.idAlunno="+request.idAlunno);
            idAlunno = request.idAlunno;
        });

        con.query(sql+con.escape(idAlunno), (err, res, cols)=>{
          if(err) throw err;

          var rows = ''; //to store html rows

          //create html table with data from res.
          for (var i = 0; i < res.length; i++) {
              rows += `<tr><td style="cursor:pointer">${res[i].voto}</td></tr>`;
          }

          var table = `<table border="1" id="table1"><tr><th>Voto</th></tr>${rows}</table>`;

          con.release();       

          return cb(table);
        });
      });
    }

//create the server for browser access
const server = http.createServer((req, res)=>{
  setResHtml(sqlAlunni, resql=>{
    reo = reo.replace('{${table}}', resql);
    res.writeHead(200, {'Content-Type':'text/html; charset=utf-8'});
    res.write(reo, 'utf-8');
    res.end();
  });
}).listen(8081);

1 个答案:

答案 0 :(得分:0)

您应该给app.listen(8081)而不是http.createServer(。请参阅https://expressjs.com/en/starter/hello-world.html