Skip to content

Instantly share code, notes, and snippets.

@pandanote-info
Created July 24, 2020 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pandanote-info/c9333fbcde4dd7f762d9f829512785aa to your computer and use it in GitHub Desktop.
Save pandanote-info/c9333fbcde4dd7f762d9f829512785aa to your computer and use it in GitHub Desktop.
Node.js用MariaDBへのSQL文の発行例のコード片。
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