-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.async.js
200 lines (146 loc) · 4.69 KB
/
app.async.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
'use strict';
const path = require('path');
const express = require('express');
const ejs = require('ejs');
const mysql = require('mysql');
const Redis = require('ioredis');
const surl = require('./core.js');
//创建应用
const app = express();
const router = express.Router();
app.listen(8089);
app.engine('.html', ejs.__express);
app.set('view engine', 'html');
//设置静态目录
app.use(express.static(path.join(__dirname, 'public')));
//更换模板目录
//app.set('views', 'temp');
//定义mysql连接选项
const mysqlOpt = {
host: 'localhost',
user: 'root',
password: '12345678',
database: 'surl'
};
app.get('/', (req, res) => {
res.render('index.html');
});
app.get('/favicon.ico', (req, res) => {
res.end();
});
app.get('/addurl/', (req, res) => {
var jsonpName = req.query.callback;
var queryURL = req.query.url;
var result = {};
var data = {};
var localSiteUrlReg = /^((http|https):\/\/)?localhost\/\w*/
var urlReg = /(http | ftp | https)/;
console.log(queryURL)
//如果添加的链接存在
if (queryURL != undefined && queryURL != '') {
var conn = mysql.createConnection(mysqlOpt);
var SQL = 'SELECT uid FROM `surl` WHERE target ="' + queryURL + '"';
//查询db中是否存在url的记录
var queryURLRecord=function(){
//console.log('unde', queryURL)
return new Promise(function(resolve,reject){
conn.query(SQL, (err, rows, fields) => {
if (err) {
console.log(err)
reject(err);
} else {
resolve(rows);
}
});
})
};
//插入到数据库中
var InsertSQL = 'INSERT INTO `surl` (target) VALUES ("' + queryURL + '")';
var insertURLRecord=function(){
return new Promise(function(resolve,reject){
conn.query(InsertSQL, (err, rows, fields) => {
if(err) {
console.log(err)
reject(err);
} else {
resolve(rows);
}
});
});
};
//async执行
(async function(){
//db中的url记录
var urlRows=await queryURLRecord();
if(urlRows.length){
var uid=urlRows[0].uid;
//返回的数据
result.code = 200;
result.url = queryURL;
result.surl = surl.idToURL(uid);
//如果是jsonp
if (jsonpName != undefined) {
res.end(jsonpName + '(' + JSON.stringify(result) + ')');
} else {
res.json(result);
}
}else{
var _insertResult=await insertURLRecord();
//返回的数据
result.code = 200;
result.url = queryURL;
result.surl = surl.idToURL(_insertResult.insertId);
console.log(result.surl)
//如果是jsonp
if (jsonpName != undefined) {
res.end(jsonpName + '(' + JSON.stringify(result) + ')');
} else {
res.json(result);
}
}
})();
} else {
data.code = 404;
data.result = null;
console.log('链接缺失')
res.end('data.code: ' + data.code);
}
});
//短网址跳转
app.get(/^\/([A-Za-z0-9]{1,6})$/, (req, res) => {
//console.log(req.params[0])
var _surl = req.params[0];
if (_surl !== 'favicon.ico') {
//连接mysql
var conn = mysql.createConnection(mysqlOpt);
var surlId = surl.URLToId(_surl);
//console.log('surlId', surlId);
var SQL = 'SELECT target FROM `surl` WHERE uid=' + surlId;
//执行查询
conn.query(SQL, (err, rows, fields) => {
if (err) {
console.log('mysql error:', err);
} else {
if (rows.length) {
var target = rows[0].target;
//重定向到相应链接
res.redirect(301, target);
res.end();
}
};
//释放mysql连接
conn.end();
});
}
});
//404
app.get('*', (req, res) => {
res.status(404).send('404');
});
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});