-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
96 lines (91 loc) · 2.26 KB
/
index.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
restful service node js
author:bonaxcrimo@gmail.com
update:10-02-2019
*/
let express = require('express');
let mysql = require('mysql');
let bodyParser = require('body-parser');
//connect to mysql
let con = mysql.createConnection({
host: 'localhost',
user: 'root',
port: 8889,
password: 'root',
database: 'manga'
});
con.connect(function(err) {
if (err) {
return console.error('error: ' + err.message);
}
console.log('Connected to the MySQL server.');
});
let app = express();
let publicDir = __dirname + '/public/';
app.use(express.static(publicDir));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//get all banner
app.get('/banner', (req, res, next) => {
con.query('select * from banner', (error, result, field) => {
con.on('error', err => {
console.log('[mysql error]', err);
});
if (result && result.length) {
res.end(JSON.stringify(result));
} else {
res.end(JSON.stringify('no banner here'));
}
});
});
//get all comic
app.get('/comic', (req, res, next) => {
con.query('select * from manga', (error, result, field) => {
con.on('error', err => {
console.log('[mysql error]', err);
});
if (result && result.length) {
res.end(JSON.stringify(result));
} else {
res.end(JSON.stringify('no comic here'));
}
});
});
//get chapter
app.get('/chapter/:mangaid', (req, res, next) => {
con.query(
'select * from chapter where MangaID=?',
[req.params.mangaid],
(error, result, field) => {
con.on('error', err => {
console.log('[mysql error]', err);
});
if (result && result.length) {
res.end(JSON.stringify(result));
} else {
res.end(JSON.stringify('no chapter here'));
}
}
);
});
//get images by chapter
app.get('/link/:chapterid', (req, res, next) => {
con.query(
'select * from link where ChapterId=?',
[req.params.chapterid],
(error, result, field) => {
con.on('error', err => {
console.log('[mysql error]', err);
});
if (result && result.length) {
res.end(JSON.stringify(result));
} else {
res.end(JSON.stringify('no image here'));
}
}
);
});
//server
app.listen(3000, () => {
console.log('listen in port 3000');
});