mysql - 使用 Node.js 和 mysql 进行工作

标签 mysql node.js

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

我得到的回复是:

  [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');
});

最佳答案

在 mysql 连接参数中添加 debug: true ,例如

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

关于mysql - 使用 Node.js 和 mysql 进行工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48123886/

相关文章:

mysql - 将一行与多行进行比较后对行求和

postgresql - 如何使用 NodeJS 将 Postgres 数据库迁移到 heroku?

javascript - 如何在继续执行脚本之前等待函数的结果?

node.js - 非虚拟机 : Cannot uninstall currently-active node version

mysql - 仅获取具有特定连接表计数的用户

php - 在 CodeIgniter 的 Active Record 查询中删除/禁用变量上的反引号

python - SQLAlchemy Core 何时进行提交?

mysql - SQL在具有多个条件的选择中对行进行排序

node.js - 从没有元信息的查询中获取结果集 Sequelize

Node.js:检测请求的所有事件何时完成