Notice
Recent Posts
Recent Comments
Link
스토리지
[06.15] Node.js 로 mySQL DB와 연결 본문
const express = require("express");
const app = express();
const port = 3000;
const mysql = require("mysql2");
const con = mysql.createConnection({
host: "localhost",
user: "root",
database: "nodejs",
password: "<PASSWORD>",
});
app.get("/", (req, res) => {
res.end("hello express");
});
app.get("/dbconnect", (req, res) => {
console.log(con);
res.end("200");
});
app.listen(port, () => {
console.log(`server is running at ${port} port.`);
});
쿼리 보내보기
const express = require("express");
const app = express();
const port = 3000;
const mysql = require("mysql2");
const con = mysql.createConnection({
host: "localhost",
user: "root",
database: "nodejs",
password: "<PASSWORD>"
});
app.get("/", (req, res) => {
res.end("hello express");
});
app.get("/:id", (req, res) => {
console.log(req.params.id);
const id = req.params.id;
const query = `select * from products where id = ${id} `;
console.log(query);
con.execute(query, (err, results, fields) => {
if (err) throw err;
const result = {
results,
status: 200,
};
const json = JSON.stringify(result);
res.send(json);
});
});
app.listen(port, () => {
console.log(`server is running at ${port} port.`);
});
'Node.js' 카테고리의 다른 글
[06.16] Sequelize (0) | 2021.06.16 |
---|---|
[06.15] mySQL Node.js 연동 (0) | 2021.06.15 |
[06.15] Database 기본 - 02(Update & Delete) (0) | 2021.06.15 |
[06.11] Database - 01 기본 (0) | 2021.06.11 |
[06.10] Router 제작 (0) | 2021.06.10 |
Comments