Created
July 24, 2020 07:02
-
-
Save pandanote-info/c9333fbcde4dd7f762d9f829512785aa to your computer and use it in GitHub Desktop.
Node.js用MariaDBへのSQL文の発行例のコード片。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mariadb = require('mariadb'); | |
const pool = mariadb.createPool({ | |
host: "(ホスト名またはホストのIPアドレス)", | |
user: "(ユーザ名)", | |
password: "(パスワード)", | |
database: "(データベース名)", | |
connectionLimit: 5 | |
}); | |
const server = http.createServer | |
(function (request, response) { | |
var urlp = request.url; | |
if (urlp.startsWith('/api/get')) { | |
response.writeHead(200, {"Content-Type": "text/plain"}); | |
var conn = pool.getConnection() | |
.then(conn => { | |
conn.query("(SQL文(SELECT文))") | |
.then(rows => { | |
response.end(JSON.stringify(rows)); | |
}) | |
.catch(err => { | |
response.end("Hello MariaDB!!\nBut there is some error(s): "+err); | |
}); | |
conn.release(); | |
}) | |
.catch(err => { | |
response.end("Hello World!!\nBut there is some error(s): "+err); | |
}); | |
} | |
// (中略) | |
}); | |
// (後略) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment