Node.js express 连接数据库

Node.js express 连接数据库

  1. 下载包 express 与 mysql
  • yarn add express yarn add mysql
  const express = require('express');
  const mysql = require('mysql');
  const app = express();
  let db = mysql.createPool({
      host: '127.0.0.1',
      user: 'root',
      password: 'root',
      database: 'test'
  });
  db.query('select * from students', (err, result)=> {
      if(err) {
          return console.log(err.message);
      }
      console.log(result)
  })
  app.listen(8080, ()=> {
      console.log('http://127.0.0.1:8080');
  })
posted @ 2022-06-26 12:08  HuangBingQuan  阅读(46)  评论(0编辑  收藏  举报