Skip to content

Commit 53694c2

Browse files
committed
using ejs
1 parent e0193fb commit 53694c2

File tree

15 files changed

+655
-70
lines changed

15 files changed

+655
-70
lines changed

5.rest-api-express/server.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ app.use('/public', express.static('public'));
2424
//app.use('/resource', express.static('resource'));
2525

2626
app.get('/',(req,res)=>{
27-
// res.end( 'server on working!');
28-
res.sendFile(__dirname + "/views/" + "index.html");
27+
res.end( 'server on working!');
28+
// res.sendFile(__dirname + "/views/" + "index.html");
2929
});
3030

3131

@@ -47,7 +47,7 @@ app.get('/students',(req,res)=>{
4747
db.queryall().then( result =>{
4848
res.send(result);
4949
console.log(result);
50-
})
50+
});
5151
});
5252

5353

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,72 @@
1-
body {padding:0;}
1+
.main {
2+
width: 80%;
3+
margin: 100px auto;
4+
/* padding-bottom: 200px; */
5+
/* background-color: #f7f7f7; */
6+
padding: 15px;
7+
}
8+
9+
.title {
10+
font-size: 17pt;
11+
/* font-style: italic; */
12+
font-weight: 600;
13+
font-family: arial;
14+
}
15+
16+
.result {
17+
color: white;
18+
display: none;
19+
padding: 30px;
20+
margin: 0;
21+
background-color: #71cfa4;
22+
}
23+
24+
input {
25+
width: 150px;
26+
height: 40px;
27+
}
28+
29+
form {
30+
background-color: #f5f5f5;
31+
padding: 30px;
32+
/* border: 1px solid red; */
33+
border-radius: 8px;
34+
}
35+
36+
.continer {
37+
display: flex;
38+
}
39+
40+
.item-same-width {
41+
flex: 1;
42+
margin: 5px;
43+
}
44+
45+
.table-header {
46+
border-radius: 8px;
47+
background-color: #529576;
48+
color: white;
49+
font-weight: 600;
50+
}
51+
52+
table {
53+
font-family: arial, sans-serif;
54+
border-collapse: collapse;
55+
border-radius: 8px;
56+
border-style: hidden;
57+
/* hide standard table (collapsed) border */
58+
box-shadow: 0 0 0 1px #71cfa4;
59+
/* this draws the table border */
60+
width: 100%;
61+
}
62+
63+
td,
64+
th {
65+
border: 1px solid #dddddd;
66+
text-align: left;
67+
padding: 8px;
68+
}
69+
70+
tr:nth-child(even) {
71+
background-color: #dddddd;
72+
}

6.rest-api-express-ajax-view/views/index.html

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -9,71 +9,9 @@
99
<script src="/public/js/jquery-3.6.3.min.js"></script>
1010

1111
<script src="/public/js/app.js"></script>
12-
<link rel="/public/css/app.css" href="styles.css">
13-
14-
<style type="text/css">
15-
.main {
16-
width: 80%;
17-
margin: 100px auto;
18-
/* padding-bottom: 200px; */
19-
/* background-color: #f7f7f7; */
20-
padding: 15px;
21-
}
22-
.title{font-size: 17pt;
23-
/* font-style: italic; */
24-
font-weight: 600;
25-
font-family: arial;}
26-
27-
.result {
28-
color: white;
29-
display: none;
30-
padding: 30px;
31-
margin: 0;
32-
background-color: #71cfa4;
33-
}
34-
35-
input {
36-
width: 150px;
37-
height: 40px;
38-
}
39-
40-
form {
41-
background-color: #f5f5f5;
42-
padding: 30px;
43-
/* border: 1px solid red; */
44-
border-radius: 8px;
45-
}
46-
47-
.continer{display: flex;}
48-
.item-same-width{flex:1;margin: 5px;}
49-
50-
.table-header{
51-
border-radius: 8px;
52-
background-color: #529576;
53-
color: white;
54-
font-weight: 600;
55-
}
56-
57-
table {
58-
font-family: arial, sans-serif;
59-
border-collapse: collapse;
60-
border-radius: 8px;
61-
border-style: hidden; /* hide standard table (collapsed) border */
62-
box-shadow: 0 0 0 1px #71cfa4; /* this draws the table border */
63-
width: 100%;
64-
}
65-
66-
td,
67-
th {
68-
border: 1px solid #dddddd;
69-
text-align: left;
70-
padding: 8px;
71-
}
72-
73-
tr:nth-child(even) {
74-
background-color: #dddddd;
75-
}
76-
</style>
12+
<link rel="stylesheet" href="/public/css/app.css">
13+
14+
7715

7816

7917

7.express-ejs-view/server.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
var path = require('path');
2+
3+
var express = require('express');
4+
var db = require('./service/database');
5+
6+
var app = express();
7+
8+
// __dirname
9+
app.set('views', path.join(__dirname, 'views'));
10+
// set the view engine to ejs
11+
app.set('view engine', 'ejs');
12+
13+
// use res.render to load up an ejs view file
14+
//app.use(expressLayouts);
15+
16+
17+
18+
19+
// index page
20+
app.get('/', function (req, res) {
21+
22+
//data transfer to index page
23+
db.queryall().then(result => {
24+
//res.send(result);
25+
console.log('result display promise:');
26+
console.log(result);
27+
console.log('render index:');
28+
29+
res.render('index',{data:result});
30+
});
31+
32+
});
33+
34+
// about page
35+
app.get('/about', function (req, res) {
36+
res.render('pages/about');
37+
});
38+
39+
app.listen(8000);
40+
console.log('Server is listening on port 8000');
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
const sql = require('mssql/msnodesqlv8')
2+
3+
4+
5+
const config = {
6+
user: 'sa',
7+
password: 'Sql132',
8+
database: 'School',
9+
server: 'localhost',
10+
pool: {
11+
max: 10,
12+
min: 0,
13+
idleTimeoutMillis: 30000
14+
},
15+
options: {
16+
encrypt: false, // for azure
17+
trustServerCertificate: true, // change to true for local dev / self-signed certs
18+
trustedConnection: true,
19+
useUTC: true
20+
}
21+
}
22+
23+
sql.on('error', function (err) {
24+
// ... error handler
25+
console.log('sql error on show : ' + err);
26+
27+
})
28+
29+
30+
31+
32+
33+
// query
34+
35+
async function query(id) {
36+
// console.log('/** query start **/');
37+
try{
38+
var pool= await sql.connect(config);
39+
var result=await pool.request().input('id', sql.Int, id).query('select * from dbo.Student where id = @id');
40+
// console.log('/** query end **/');
41+
return result.recordset;
42+
}
43+
catch(error)
44+
{console.log(`query error : ${error}`); }
45+
46+
}
47+
48+
async function queryall() {
49+
// console.log('/** query start **/');
50+
try{
51+
var pool= await sql.connect(config);
52+
var result=await pool.request().query('select * from dbo.Student');
53+
// console.log('/** query end **/');
54+
return result.recordset;
55+
}
56+
catch(error)
57+
{console.log(`query error : ${error}`); }
58+
59+
}
60+
61+
62+
63+
//insert
64+
65+
function insert(name, department) {
66+
console.log('/** insert start **/');
67+
sql.connect(config).then(connection => {
68+
// Query
69+
console.log('connect show : ');
70+
var poolrec = connection.request();
71+
var q = poolrec.query(`insert into Student (student_name,department) values ('${name}','${department}')`)
72+
return q;
73+
74+
}).then(result => {
75+
console.log('inserted rows count: ' + result.rowsAffected);
76+
}).catch(err => {
77+
// ... error checks
78+
console.log('catch show : ' + err);
79+
});
80+
81+
console.log('/** insert end **/');
82+
}
83+
84+
85+
// insert using transaction
86+
87+
function insert_trans(id, department) {
88+
const pool = new sql.ConnectionPool(config);
89+
const transaction = new sql.Transaction(pool)
90+
transaction.begin(err => {
91+
// ... error checks
92+
93+
const request = new sql.Request(transaction)
94+
request.query('insert into Student (department) values (' + department + ') where id=' + id, (err, result) => {
95+
// ... error checks
96+
97+
transaction.commit(err => {
98+
// ... error checks
99+
100+
console.log("Transaction committed.")
101+
})
102+
})
103+
})
104+
}
105+
106+
107+
// update
108+
109+
function update(id, department) {
110+
console.log('/** update start **/');
111+
112+
sql.connect(config).then(connection => {
113+
// Query
114+
var count = 0;
115+
const request = new sql.Request()
116+
request.stream = true
117+
return request.input('id', sql.Int, id).query(`update Student set department='${department}' where id=@id`);
118+
119+
}).then(result => {
120+
console.log('update result rows count : ' + result.rowsAffected);
121+
122+
}).catch(err => {
123+
// ... error checks
124+
console.log('catch show : ' + err);
125+
});
126+
console.log('/** update end **/');
127+
}
128+
129+
130+
131+
//insert('jack','biology');
132+
//update(3,'oop');
133+
//query(2).then(result=>{console.log(result) ;});
134+
135+
136+
137+
module.exports = { query, queryall , insert, update };
138+

0 commit comments

Comments
 (0)