728x90
const express = require("express");
const app = express();
const server = app.listen(3008, () => {
console.log("Start Server: localhost: 3008");
})
/******************************************************/
//POST방식을 사용하려면 우선 npm install body-parser 설치
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("/insert", function(req,res){ //get방식
getInsertDeptGet(req, res);
})
app.post("/insert", function(req,res){ //post방식
getInsertDeptPost(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>"
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>"
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 getInsertDeptGet(req, res){
oracledb.autoCommit = true;
let deptno = req.param("deptno");
let dname = req.param("dname");
let loc = req.param("loc");
console.log(deptno, dname, loc)
try {
connection = oracledb.getConnection({
user: dbconfig.user,
password: dbconfig.password,
connectString: dbconfig.xid
})
const result = (await connection).execute("insert into dept(deptno, dname, loc) values (:deptno, :dname, :loc)", [deptno, dname, loc],
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);
}
}
}
}
async function getInsertDeptPost(req, res){
oracledb.autoCommit = true;
let deptno = req.param("deptno");
let dname = req.param("dname");
let loc = req.param("loc");
console.log(deptno, dname, loc)
try {
connection = oracledb.getConnection({
user: dbconfig.user,
password: dbconfig.password,
connectString: dbconfig.xid
})
const result = (await connection).execute("insert into dept(deptno, dname, loc) values (:deptno, :dname, :loc)", [deptno, dname, loc],
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);
}
}
}
}
Get 방식
POST 방식
'단순 코드 기록 > Node' 카테고리의 다른 글
Node_Template 사용 + Update (0) | 2024.03.26 |
---|---|
Node_pathVariable_Delete (0) | 2024.03.26 |
Node_metaData로 컬럼 뽑기 / req.param으로 데이터 뽑기 (0) | 2024.03.26 |
Node_99번 부서 중복insert/update/delete/Table (0) | 2024.03.25 |
Node_10번 부서 출력 + insert (0) | 2024.03.25 |