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 Show 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ả 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 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 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) Cài đặt mô-đun var mysql = require('mysql');3 bằng lệnh var mysql = require('mysql');4 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 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
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 modelsQuan hệ giữa 2 bảng 7 và 8 là 1-n. Một Todo có nhiều TodoItem và một TodoItem thuộc về một Todo. Run command afterconnection.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 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 8 ta chạy lệnh sauconnection.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 địnhCreate todoControllerTạ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 đườngEdit 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 List of the todosThê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; 0And results on postman is Cập nhật một việc cần làmTươ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 1Add 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 2Kết quả người đưa thư Clear todosCuố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 3Thêm tuyến đường 4Nế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 5Kết quả 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 |