var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app).listen(80);
var mysql = require('mysql');
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({limit: '50mb', extended: false}));
app.use(bodyParser.json({limit: '50mb'}));
var connection = mysql.createConnection({
host: 'localhost'
, port: 3306
, user: 'root'
, password: 'root'
, database: 'test'
});
app.get('/', function (req, res) {
res.sendfile("main.html");
});
app.get('/main', function (req, res) {
res.sendfile("main.html");
});
app.get('/insertAircraftPage', function (req, res) {
res.sendfile("insertAircraft.html");
});
app.get('/insertFlightPage', function (req, res) {
res.sendfile("insertFlight.html");
});
app.get('/flight', function (req, res) {
var q = ` select * from flight f, aircraft a where f.aircraftCode = a.aircraftCode;`;
connection.query(q,
function (err, rows, fields) {
if (err) throw err;
res.send(rows);
}
);
});
app.post('/flight', function (req, res) {
var flightName = req.body.flightName;
var aircraftCode = req.body.aircraftCode;
var departure = req.body.departure;
var arrival = req.body.arrival;
var departTime = req.body.departTime;
var arriveTime = req.body.arriveTime;
var q = `insert into flight (flightName, aircraftCode, departure, arrival, departTime, arriveTime)
values
("${flightName}", "${aircraftCode}", "${departure}", "${arrival}", "${departTime}", "${arriveTime}");`;
connection.query(q,
function (err, rows, fields) {
if (err) throw err;
res.send(rows);
}
);
});
app.get('/aircraft', function (req, res) {
var q = `select aircraftCode, aircraftName from aircraft;`;
connection.query(q,
function (err, rows, fields) {
if (err) throw err;
res.send(rows);
}
);
});
app.post('/aircraft', function (req, res) {
var aircraftCode = req.body.aircraftCode;
var aircraftName = req.body.aircraftName;
var seats = req.body.seats;
var q = `insert into aircraft (aircraftCode, aircraftName, seats) values ("${aircraftCode}", "${aircraftName}", ${seats});`;
connection.query(q,
function (err, rows, fields) {
if (err) throw err;
res.send(rows);
}
);
});
// var q = `;`;
// connection.query(q,
// function (err, rows, fields) {
// if (err) throw err;
// res.send(rows);
// }
// );
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
항공코드 <input type="text" id="aircraftCode"><br>
항공기명 <input type="text" id="aircraftName"><br>
좌석수 <input type="text" id="seats"><br>
<input type="button" value="항공기 입력" id="insertAircraftBtn">
<input type="button" value="메인으로" id="backToMainBtn">
</body>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$("#insertAircraftBtn").click(function(){
$.ajax({
url:'/aircraft',
type:"POST",
data:{
aircraftCode: $("#aircraftCode").val(),
aircraftName: $("#aircraftName").val(),
seats: $("#seats").val(),
},
success:function(res){
alert("항공기 정보가 입력되었습니다.");
location.href = "/";
}
});
});
$("#backToMainBtn").click(function(){
location.href = "/";
});
</script>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
편명<input type="text" id="flightName"><br>
항공기명 <select id="aircraftCode">
</select><br>
출발지<input type="text" id="departure"><br>
목적지<input type="text" id="arrival"><br>
출발시각<input type="text" id="departTime"><br>
도착시각<input type="text" id="arriveTime"><br>
<input type="button" value="운항 입력" id="insertFlightBtn">
<input type="button" value="메인으로" id="backToMainBtn">
</body>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$.ajax({
url:'/aircraft',
data:{
},
success:function(res){
for(var i=0;i<res.length;i++) {
var aircraft = res[i];
$("#aircraftCode").append(`<option value="${aircraft.aircraftCode}">${aircraft.aircraftName}</option>`)
}
}
});
$("#insertFlightBtn").click(function(){
$.ajax({
url:'/flight',
type:"POST",
data:{
flightName: $("#flightName").val(),
aircraftCode: $("#aircraftCode").val(),
departure: $("#departure").val(),
arrival: $("#arrival").val(),
departTime: $("#departTime").val(),
arriveTime: $("#arriveTime").val(),
},
success:function(res){
alert("운항편이 입력되었습니다.");
location.href = "/";
}
});
});
$("#backToMainBtn").click(function(){
location.href = "/";
});
</script>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div id="flightArea"></div>
<br>
<input type="button" value="항공기 입력" id="goToInsertAircraftPage">
<input type="button" value="운항 입력" id="goToInsertFlightPage">
</body>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$.ajax({
url:'/flight',
data:{
},
success:function(res){
console.log(res);
$("#flightArea").append(`편명 / 항공기명 / 좌석수 / 출발지 / 목적지 / 출발시각 / 도착시각<br><br>`)
for(var i=0;i<res.length;i++) {
var flight = res[i];
$("#flightArea").append(`${flight.flightName} / ${flight.aircraftName} / ${flight.seats} / ${flight.departure} / ${flight.arrival} / ${flight.departTime} / ${flight.arriveTime} <br>`)
}
}
});
$("#goToInsertAircraftPage").click(function(){
location.href = "/insertAircraftPage";
});
$("#goToInsertFlightPage").click(function(){
location.href = "/insertFlightPage";
});
</script>
</html>
'개발자 > Javascript' 카테고리의 다른 글
Javascript (자바스크립트) 구글차트 (0) | 2020.06.18 |
---|---|
아톰 세팅 하이라이트 & 미니맵 설치하기 & 단축키 (0) | 2020.06.18 |
Javascript (자바스크립트) ajax 계산기, 자동차 구입, 3개의 수 중 가장 큰 수 구하기 (0) | 2020.06.18 |
Javascript (자바스크립트) jQuery and ajax (0) | 2020.06.18 |
node.js 접속 쿼리 분해 (0) | 2020.05.14 |