# \[GRAPHQL] Tạo Database với Mysql (ok)

{% file src="https://2726517656-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M1E4Gk2ppVKb4olmnun%2F-MUHwK7fjnhSQqfEuFM2%2F-MUHwtLzQBije2R5P_FU%2Fnodejs_api.sql?alt=media&token=51fd33ba-2d98-4b5d-9c94-a2058308bc0b" %}

![](https://2726517656-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M1E4Gk2ppVKb4olmnun%2F-MUHvDZCWOUYniSWQuix%2F-MUHvfXUzwnB48YpCkEw%2FScreenshot_2.jpg?alt=media\&token=dbedb6e9-542c-4055-86fc-0e83b7bff56b)

C:\mysql\_demo\package.json

```
{
  "name": "mysql_demo",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "dependencies": {
    "express": "^4.17.1",
    "mysql": "^2.10.2"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
```

C:\mysql\_demo\server.js

```
var express = require("express");
var mysql = require("mysql"); // include thêm module mysql
var app = express();
// Tạo kết nối với Database
var pool = mysql.createPool({
  host: "localhost",
  user: "root",
  password: null,
  database: "nodejs_api",
});
app.get("/user", function(req, res) {
  // Viết câu truy vấn sql
  var sql = "SELECT * FROM `products`"; // Thực hiện câu truy vấn và show dữ liệu
  pool.query(sql, function(error, result) {
    if (error) throw error;
    console.log("USER TABLE", result);
    res.json(result); // Trả kết quả về cho client dưới dạng json
  });
});
app.listen("3000", "127.0.0.1");
console.log("server is listening");
```
