728x90
const express = require("express");
const app = express();
const server = app.listen(3009, () => {
console.log("Start Server: localhost: 3009");
})
app.use(express.urlencoded({extended: true}));
app.set("views", __dirname + "/views");
app.set("view engine", "ejs");
app.engine("html", require("ejs").renderFile);
app.get("/", function(req, res){
res.render("index2.html");
})
app.get("/insertForm", function(req, res){
res.render("insertForm.html");
})
const oracledb = require("oracledb");
oracledb.initOracleClient();
const dbconfig = require("./dbconfig.js");
app.get("/table", function(req, res){
getTable(req, res);
})
app.get("/selectOne", function(req,res){
getSelectOne(req, res);
})
/* ***************************************** */
app.get("/delete/:deptno", function(req,res){
getDelete(req, res);
})
/* **************************************** */
async function getTable(req, res){
let connection;
try {
connection = oracledb.getConnection({
user: dbconfig.user,
password: dbconfig.password,
connectString: dbconfig.xid
})
const result = (await connection).execute("select * from dept order by deptno",
function(error, data){
if(error){
console.log(error);
}
var headName = "";
for (let index = 0; index < data.metaData.length; index++) {
headName += "<th>"+data.metaData[index].name+"</th>"
}
var dataSet = "";
for (let index = 0; index < data.rows.length; index++) {
var dataes = "";
dataes += "<td><a href='/selectOne?deptno="+data.rows[index][0]+"'>"
+data.rows[index][0]+"</a></td>"
+ "<td>"+data.rows[index][1]+"</td>"
+ "<td>"+data.rows[index][2]+"</td>"
/* ********************************************************* */
+ "<td><a href='/delete/"+data.rows[index][0]+"'>삭제</a></td>"
/* ********************************************************* */
dataSet += "<tr>"+dataes+"</tr>";
}
var resultData = "<html><head><body><table border='1'>"
+"<tr>"+headName+"</tr>"
+dataSet
+"</table></body></head></html>"
res.send(resultData);
});
} catch (error) {
console.log(error);
} finally {
if(connection){
try{
(await connection).close
} catch(error){
console.log("error: ", error);
}
}
}
}
async function getSelectOne(req, res){
let connection;
let deptno = req.param("deptno");
try {
connection = oracledb.getConnection({
user: dbconfig.user,
password: dbconfig.password,
connectString: dbconfig.xid
})
const result = (await connection).execute("select * from dept where deptno=:num order by deptno", [deptno],
function(error, data){
if(error){
console.log(error);
}
var headName = "";
for (let index = 0; index < data.metaData.length; index++) {
headName += "<th>"+data.metaData[index].name+"</th>"
}
var dataSet = "";
for (let index = 0; index < data.rows.length; index++) {
var dataes = "";
dataes += "<td><a href='/selectOne?deptno="+data.rows[index][0]+"'>"
+data.rows[index][0]+"</a></td>"
+ "<td>"+data.rows[index][1]+"</td>"
+ "<td>"+data.rows[index][2]+"</td>"
/* ********************************************************* */
+ "<td><a href='/delete/"+data.rows[index][0]+"'>삭제</a></td>"
/* ********************************************************* */
dataSet += "<tr>"+dataes+"</tr>";
}
var resultData = "<html><head><body><table border='1'>"
+"<tr>"+headName+"</tr>"
+dataSet
+"</table></body></head></html>"
res.send(resultData);
}
);
} catch (error) {
console.log(error);
} finally {
if(connection){
try{
(await connection).close
} catch(error){
console.log("error: ", error);
}
}
}
}
async function getDelete(req, res){
oracledb.autoCommit = true;
/* ************************************ */
let deptno = req.params.deptno;
console.log("pathVariable data: ", deptno)
/* ************************************ */
try {
connection = oracledb.getConnection({
user: dbconfig.user,
password: dbconfig.password,
connectString: dbconfig.xid
})
const result = (await connection).execute("delete from dept where deptno = :num", [deptno],
function(error, result){
console.log("실행됨");
if(error){
console.log(error);
res.send("삭제 실패");
}
console.log(result); //{ lastRowid: '로우키', rowsAffected: 입력된 행 수}
if(result.rowsAffected == 1){
res.send("<a href='/table'>목록 보기</a>");
}
}
);
} catch (error) {
console.log(error);
} finally {
if(connection){
try{
(await connection).close
} catch(error){
console.log("error: ", error);
}
}
}
}
'단순 코드 기록 > Node' 카테고리의 다른 글
Node_소켓통신을 이용한 서버 가동 (0) | 2024.03.27 |
---|---|
Node_Template 사용 + Update (0) | 2024.03.26 |
Node_Form을 이용한 Insert (0) | 2024.03.26 |
Node_metaData로 컬럼 뽑기 / req.param으로 데이터 뽑기 (0) | 2024.03.26 |
Node_99번 부서 중복insert/update/delete/Table (0) | 2024.03.25 |