Mysql-npm

Việc kết nối Mysql xampp của nodejs thực sự rất đơn giản và dễ hiểu. Đầu tiên bạn cài đặt mô-đun như sau

npm cài đặt mysql

Sau khi cài đặt, bạn viết câu lệnh kết nối, bạn cần đặt tên máy chủ, người dùng, mật khẩu, cơ sở dữ liệu

//tao kết nối mysql
var connection = mysql.createConnection({
	host:'localhost',
	user:'skipperhoa',
	password:'0975595084',
	database:'basic_nodejs'
});

- Để thực hiện một câu lệnh, hãy chọn những người bạn làm như sau

connection.query('select * from sinhvien', function (error, results, fields) {
	  if (error) throw error;
	  res.render('index',{results});
	});

- Các bạn chú ý. dòng lệnh chọn, Hiện tại tôi có một bảng sinhvien, tôi chọn bảng này và hiển thị lượt xem cho người dùng xem

Code ví dụ Node. js MySQL – Tạo cơ sở dữ liệu, kết nối cơ sở dữ liệu

(Xem lại. Hướng dẫn cài đặt MySQL)

(Xem lại. Code ví dụ kết nối Node. js với MySQL)

Tạo cơ sở dữ liệu MySQL với Node. js

Ở đây mình sẽ tạo cơ sở dữ liệu với tên là “demo”

Câu lệnh sql sẽ là 

var mysql = require('mysql');
5

Thực hiện kết nối tới MySQL và trong truy vấn phương thức thì thực hiện câu sql trên

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "admin1234"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

var query = "CREATE DATABASE demo"

con.query(query, function (err, result) {
    if (err) throw err;
    console.log("Database created");
 });
  
con.end(function(err) {
  if (err) throw err;
  console.log("Closed!");
});

Lưu đoạn mã trên file

var mysql = require('mysql');
6 và chạy (nhớ phải cài đặt module mysql trước nhé)

Kết quả

Mysql-npm

Mysql-npm

Nếu bạn muốn xóa cơ sở dữ liệu

var mysql = require('mysql');
7 từ nút. js thì chỉ việc thay câu truy vấn thành _______58

Sau khi tạo cơ sở dữ liệu

var mysql = require('mysql');
7 ta thực hiện kết nối với nó

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  port: "3306",
  user: "root",
  password: "admin1234",
  database: "demo"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

con.end(function(err) {
  if (err) throw err;
  console.log("Closed!");
});

Lưu đoạn mã trên tệp

var mysql = require('mysql');
0 và chạy

Mysql-npm

Dưới đây là nút dự án. js mình sáng tạo để kết nối và tạo cơ sở dữ liệu với mysql

Mysql-npm

Code ví dụ Node. js MySQL – Tạo cơ sở dữ liệu, kết nối cơ sở dữ liệu stackjava. com

Được rồi, Xong

Tải xuống mã ví dụ trên tại đây

 

Người giới thiệu

https. //www. w3schools. com/nodejs/nodejs_mysql_create_db. asp

Ở đây mình tạo project Node. js ở thư mục 

var mysql = require('mysql');
1t do that mình sẽ mở màn hình cmd và cd tới thư mục đó

Khởi tạo dự án Node. js with default information information by

var mysql = require('mysql');
2

(Xem lại. Tạo nút dự án. js với npm)

Mysql-npm

Cài đặt mô-đun

var mysql = require('mysql');
3 bằng lệnh
var mysql = require('mysql');
4

Mysql-npm

Hoặc bạn cũng có thể cài đặt mô-đun mysql bằng cách khai báo phụ thuộc 

var mysql = require('mysql');
0 trong tệp
var mysql = require('mysql');
1

Tạo tệp

var mysql = require('mysql');
2

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  port: "3307",
  user: "root",
  password: "admin1234"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

con.end(function(err) {
  if (err) throw err;
  console.log("Closed!");
});

Trong bài này mình không kết nối riêng với cơ sở dữ liệu nào cả. Đến bài sau mình sẽ thực hiện việc tạo cơ sở dữ liệu, kết nối tới cơ sở dữ liệu và thực hiện truy vấn

Sau đó ta tạo một tệp có tên

DEBUG=myapp:* npm start
6 trong thư mục gốc của dự án. Trong tệp này, chúng tôi sẽ chỉ định đường dẫn (đường dẫn) được yêu cầu bởi Sequelize

const path = require('path');

module.exports = {
  "config": path.resolve('./config', 'config.json'),
  "models-path": path.resolve('./models'),
  "seeders-path": path.resolve('./seeders'),
  "migrations-path": path.resolve('./migrations')
};

Then next ta run command

connection.query('select * from sinhvien', function (error, results, fields) {
	  if (error) throw error;
	  res.render('index',{results});
	});
0

để cài đặt gói cho mysql. Lệnh sau sẽ tạo các mẫu mã cho kết nối đến DB của chúng tôi

connection.query('select * from sinhvien', function (error, results, fields) {
	  if (error) throw error;
	  res.render('index',{results});
	});
1

Ta nhìn lại thì cấu trúc dự án sẽ như thế này

connection.query('select * from sinhvien', function (error, results, fields) {
	  if (error) throw error;
	  res.render('index',{results});
	});
2

Tiếp theo ta tạo bộ điều khiển thư mục trong thư mục gốc của dự án

connection.query('select * from sinhvien', function (error, results, fields) {
	  if (error) throw error;
	  res.render('index',{results});
	});
30

Create the models

Quan hệ giữa 2 bảng

DEBUG=myapp:* npm start
7 và
DEBUG=myapp:* npm start
8 là 1-n. Một Todo có nhiều TodoItem và một TodoItem thuộc về một Todo. Run command after

connection.query('select * from sinhvien', function (error, results, fields) {
	  if (error) throw error;
	  res.render('index',{results});
	});
31

Câu lệnh này sẽ sinh ra 1 tệp todo. js trong thư mục

DEBUG=myapp:* npm start
9 và tệp di chuyển tệp
connection.query('select * from sinhvien', function (error, results, fields) {
	  if (error) throw error;
	  res.render('index',{results});
	});
80 trong thư mục
connection.query('select * from sinhvien', function (error, results, fields) {
	  if (error) throw error;
	  res.render('index',{results});
	});
81.
connection.query('select * from sinhvien', function (error, results, fields) {
	  if (error) throw error;
	  res.render('index',{results});
	});
82 sẽ là mô hình thời gian được tạo ra. Todo model will like after

connection.query('select * from sinhvien', function (error, results, fields) {
	  if (error) throw error;
	  res.render('index',{results});
	});
32

Tương tự với bảng

DEBUG=myapp:* npm start
8 ta chạy lệnh sau

connection.query('select * from sinhvien', function (error, results, fields) {
	  if (error) throw error;
	  res.render('index',{results});
	});
33

Cuối cùng chúng ta chạy lệnh di chuyển để tạo di chuyển

connection.query('select * from sinhvien', function (error, results, fields) {
	  if (error) throw error;
	  res.render('index',{results});
	});
34

Tạo bộ điều khiển và sửa định tuyến mặc định

Create todoController

Tạo 1 tệp

connection.query('select * from sinhvien', function (error, results, fields) {
	  if (error) throw error;
	  res.render('index',{results});
	});
84 trong thư mục
connection.query('select * from sinhvien', function (error, results, fields) {
	  if (error) throw error;
	  res.render('index',{results});
	});
85. Trong tệp này có thêm chức năng
connection.query('select * from sinhvien', function (error, results, fields) {
	  if (error) throw error;
	  res.render('index',{results});
	});
86

connection.query('select * from sinhvien', function (error, results, fields) {
	  if (error) throw error;
	  res.render('index',{results});
	});
35

Tiếp theo chúng ta tạo tệp

connection.query('select * from sinhvien', function (error, results, fields) {
	  if (error) throw error;
	  res.render('index',{results});
	});
87 trong
connection.query('select * from sinhvien', function (error, results, fields) {
	  if (error) throw error;
	  res.render('index',{results});
	});
85 để xuất bộ điều khiển

connection.query('select * from sinhvien', function (error, results, fields) {
	  if (error) throw error;
	  res.render('index',{results});
	});
36

Chỉnh sửa tuyến đường

Edit file

connection.query('select * from sinhvien', function (error, results, fields) {
	  if (error) throw error;
	  res.render('index',{results});
	});
87 in directory
var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  port: "3306",
  user: "root",
  password: "admin1234",
  database: "demo"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

con.end(function(err) {
  if (err) throw err;
  console.log("Closed!");
});
30 to as after

connection.query('select * from sinhvien', function (error, results, fields) {
	  if (error) throw error;
	  res.render('index',{results});
	});
37

Như vậy chúng ta vừa tạo route cho phương thức POST to

connection.query('select * from sinhvien', function (error, results, fields) {
	  if (error) throw error;
	  res.render('index',{results});
	});
86 Todo. Tiếp theo chúng ta thêm route này vào tệp
var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  port: "3306",
  user: "root",
  password: "admin1234",
  database: "demo"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

con.end(function(err) {
  if (err) throw err;
  console.log("Closed!");
});
32 của ứng dụng

Ta edit file

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  port: "3306",
  user: "root",
  password: "admin1234",
  database: "demo"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

con.end(function(err) {
  if (err) throw err;
  console.log("Closed!");
});
32 as after

connection.query('select * from sinhvien', function (error, results, fields) {
	  if (error) throw error;
	  res.render('index',{results});
	});
38

Chú ý phải được thêm vào trước ứng dụng. được('',. ) vì ứng dụng sẽ tự tìm kiếm các tuyến theo thứ tự để khớp, nếu không phù hợp mới chuyển sang tuyến ở bên dưới. ứng dụng. được('',. ) là bắt tất cả các yêu cầu và trả lại tin nhắn luôn

Use postman for a todo

Hướng dẫn npm mysql

List of the todos

Thêm chức năng

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  port: "3306",
  user: "root",
  password: "admin1234",
  database: "demo"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

con.end(function(err) {
  if (err) throw err;
  console.log("Closed!");
});
34 todo vào bộ điều khiển
var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  port: "3306",
  user: "root",
  password: "admin1234",
  database: "demo"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

con.end(function(err) {
  if (err) throw err;
  console.log("Closed!");
});
35 sau phương thức
connection.query('select * from sinhvien', function (error, results, fields) {
	  if (error) throw error;
	  res.render('index',{results});
	});
86

connection.query('select * from sinhvien', function (error, results, fields) {
	  if (error) throw error;
	  res.render('index',{results});
	});
39

Tiếp theo mở tệp

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  port: "3306",
  user: "root",
  password: "admin1234",
  database: "demo"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

con.end(function(err) {
  if (err) throw err;
  console.log("Closed!");
});
37 để tạo 1 bản đồ URL với yêu cầu GET để liệt kê danh sách các todos;

DEBUG=myapp:* npm start
0

And results on postman is

Hướng dẫn npm mysql

Cập nhật một việc cần làm

Tương tự chúng ta bổ sung hàm

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  port: "3306",
  user: "root",
  password: "admin1234",
  database: "demo"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

con.end(function(err) {
  if (err) throw err;
  console.log("Closed!");
});
38 việc cần làm trong
var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  port: "3306",
  user: "root",
  password: "admin1234",
  database: "demo"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

con.end(function(err) {
  if (err) throw err;
  console.log("Closed!");
});
35 dưới
var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  port: "3306",
  user: "root",
  password: "admin1234",
  database: "demo"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

con.end(function(err) {
  if (err) throw err;
  console.log("Closed!");
});
34 như sau

DEBUG=myapp:* npm start
1

Add route in

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  port: "3306",
  user: "root",
  password: "admin1234",
  database: "demo"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

con.end(function(err) {
  if (err) throw err;
  console.log("Closed!");
});
37

DEBUG=myapp:* npm start
2

Kết quả người đưa thư

Hướng dẫn npm mysql

Clear todos

Cuối cùng chúng ta tạo chức năng xóa việc cần làm

Thêm đoạn mã vào tệp

var mysql = require('mysql');
12

DEBUG=myapp:* npm start
3

Thêm tuyến đường

DEBUG=myapp:* npm start
4

Nếu kiểm tra bằng POSTMAN chúng ta có thể ngạc nhiên vì không thấy bất kỳ dữ liệu nào được trả về. Sửa đổi một chút mã trả về một trạng thái = 200 và một thông báo xóa thành công

DEBUG=myapp:* npm start
5

Kết quả

Hướng dẫn npm mysql

Như vậy, ta đã tạo một ứng dụng nodejs viết api đơn giản. Tùy thuộc vào từng dự án có mức độ bảo mật khác nhau, ta có thể sử dụng khóa mã thông báo để xác minh yêu cầu hoặc cho phép máy chủ ip để không ảnh hưởng đến thông tin của dự án và hạn chế truy cập lai có ý kiến