Node.js는 고성능 웹 서버를 작성하는 데 사용되는 JavaScript 툴킷입니다.
일반적으로 NodeJS 개발에서는 종종 데이터베이스, 특히 MySQL을 작동합니다. 가장 널리 사용되는 오픈 소스 데이터베이스로서 우리의 첫 번째 선택이되었습니다. 이 기사는 NodeJS를 통해 MySQL 데이터베이스를 작동하는 방법을 소개합니다. MySQL 모듈을 Nodejs에 설치하려면 Nodejs가 MySQL을 지원해야하며 시스템 지원 라이브러리에 MySQL 모듈을 추가해야합니다.
node.js를 빠르게 이해하려면 node.js_guide.pdf-node.js 개발 안내서를 살펴보십시오.
메시지를 남기고 싶지 않다면 비행기를 만들 수 있습니다! 직접 다운로드하십시오
node.js
Node.js의 작동을 간단히 소개하겠습니다
Node-Mysql을 설치하십시오
C 코드
$ npm MySQL을 설치하십시오
테스트 테이블을 만듭니다
// 데이터베이스 이름 노드 샘플
C 코드
테이블 만들기`nodesample`.`mytable` ( 'id` int int signed not null auto_increment 기본 키,`FirstName` varchar (20) null,`lastname` varchar (20) null,`텍스트 null이 아닌 텍스트) 엔진 = myisam;
데이터베이스에 연결하십시오
JS 코드
var sys = 요구 ( 'sys'); var client = require ( 'mysql'). 클라이언트; var client = new Client (); client.user = 'somesuser'; client.Password = '비밀번호'; client.connect (function (error, results) {if (error) {console.log ( 'connection error :' + error.message); return;} console.log ( 'mysql에 연결');});데이터베이스를 엽니 다
JS 코드
clientConnectionready = function (client) {client.query ( 'nodesample', function (error, results) {if (error) {console.log ( 'clientConnectionready error :' + error.message); client.end (); return;}}); };데이터베이스 작동 절차를 완료합니다
JS 코드
var sys = 요구 ( 'sys'); var client = require ( 'mysql'). 클라이언트; var client = new Client (); client.user = 'somesuser'; client.Password = '비밀번호'; Console.log ( 'MySQL에 연결 ...'); client.connect (function (error, results) {if (error) {console.log ( 'connection error :' + error.message); return;} console.log ( 'mysql에 연결'); clientConnectionready (client);}); clientConnectionready = function (client) {client.query ( 'nodesample', function (error, results) {if (error) {console.log ( 'clientConnectionready error :' + error.message); client.end ();} client (client);}); }; ClientReady = function (client) {var value = [ 'chad', 'lung', 'hello world']; client.query ( 'myTable set firstName =?, lastName =?, message =?', value, function (error, results) {if (error) {console.log ( "clientready error :" + error.message); client.end (); return;} console.log ( 'inserted :'insertedrows + 'row.'); results.insertid); getData (클라이언트); } getData = function (client) {client.query ( 'select * from mytable', function selectCb (오류, 결과, 필드) {if (error) {console.log ( 'getData Error :' + error.message); client.end (); return; //console.log. Console.log ( '성 :' + firstresult [ 'lastname']); client.end (); Console.log ( '연결 폐쇄'); };