-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
80 lines (73 loc) · 1.98 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
*
* @Name : MysqlExpressJs/app.js
* @Version : 1.0
* @Programmer : Max
* @Date : 2019-09-06
* @Released under : https://github.com/BaseMax/MysqlExpressJs/blob/master/LICENSE
* @Repository : https://github.com/BaseMax/MysqlExpressJs
*
**/
let express = require('express')
let cookieParser = require('cookie-parser')
let bodyParser = require('body-parser')
let multer = require('multer')
let app = express()
var mysql = require('mysql')
const util = require('util')
// let MysqlJson = require('mysql-json')
var db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'asrez'
})
db.connect((err) => {
if(err) {
throw err;
}
console.log('Connected to database');
});
global.db = db;
const query = util.promisify(db.query).bind(db);
let data = {
countPosts:async () => {
const rows = await query('select count(*) as count from `post`');
console.log(rows);
},
getPosts:async () => {
const posts = await query('select * from `post` ORDER BY `id` DESC');
for(let i=0;i<posts.length;i++) {
posts[i]["tags"]= await query('select * from `post_tag` WHERE `id` = '+ posts[i].id +' ORDER BY `id` DESC');
}
console.log(posts);
return posts
}
}
// app.use(multer({ dest: './upload/' }))
app.use(cookieParser())
app.use(express.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
// app.use(bodyParser.json())
// app.use(bodyParser.urlencoded({
// extended: true
// }))
// app.use(bodyParser())
app.set('view engine', 'pug')
app.set('views', './view')
app.use(express.static('static'))
let config= {
site:'http://localhost:8081/',
}
app.get('/', async (request, response) => {
data.countPosts();
let posts=data.getPosts();
response.render('main', {config:config, posts:posts})
// return response.status(500).send("Error!");
})
let server = app.listen(8081, function () {
let host = server.address().address
let port = server.address().port
console.log("MysqlExpressJs App listening at http://%s:%s", host, port)
})