使用node.js和mysql进行Woking

时间:2018-01-06 03:04:36

标签: mysql node.js

//所以我在节点和表达框架中使用mysql,第一次创建测试示例时一切正常。但后来我尝试创建第二个项目,现在路由似乎没有被读取。

我得到的回复是:

  [nodemon] restarting due to changes...
  [nodemon] starting `node app.js`
  Server started on port 8000
  mysql connected...

//我也应该得到结果:

OkPackege {...     ...     ...     ...     }

//But I am not getting it. Any Ideas...? thanks.

我的口述如下:

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

const db = mysql.createConnection({
  host      : 'localhost',
  user      : 'root',
  password  : 'LEoking1987'
  //database  : 'nodesql'
});

db.connect((err) => {
  if(err){
      throw err;
  }
  console.log('mysql connected...');
});

const app = express();

// Creates satabase if it does not exist yet.
app.get('/createdb',(req,res) => {
    let sql = 'CREATE DATABASE nodesql';
    db.query(sql, (err, result) => {
      if(err) throw err;
      console.log(result);
      res.send('Database created...');
    });
});

app.listen('8000',()=>{
  console.log('Server started on port 8000');
});

1 个答案:

答案 0 :(得分:0)

在你的mysql连接中添加debug:true,如

  mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'LEoking1987'
  database: 'nodesql'
  debug: true,     
})