forked from pct/moedict-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
103 lines (92 loc) · 2.56 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// no longer being Generated by CoffeeScript 1.3.3
(function() {
var fs = require('fs'),
express = require('express'),
async = require('async'),
Sequelize = require('sequelize');
var app = express();
var sql = new Sequelize('database', 'username', 'password', {
dialect: 'sqlite',
storage: './db.sqlite3',
logging: false,
define: {
underscored: true,
timestamps: false
}
}),
Dict = sql.define('dicts', {
id: Sequelize.INTEGER,
name: Sequelize.STRING,
type: Sequelize.INTEGER
}),
Entry = sql.define('entries', {
id: Sequelize.INTEGER,
title: Sequelize.STRING,
radical: Sequelize.STRING,
stroke_count: Sequelize.INTEGER,
non_radical_stroke_count: Sequelize.INTEGER,
dict_id: Sequelize.INTEGER
}),
Heteronym = sql.define('heteronyms', {
id: Sequelize.INTEGER,
entry_id: Sequelize.INTEGER,
bopomofo: Sequelize.STRING,
bopomofo2: Sequelize.STRING,
pinyin: Sequelize.STRING
}),
Definition = sql.define('definitions', {
id: Sequelize.INTEGER,
heteronym_id: Sequelize.INTEGER,
type: Sequelize.STRING,
def: Sequelize.STRING,
example: Sequelize.STRING,
synonyms: Sequelize.STRING,
antonyms: Sequelize.STRING,
quote: Sequelize.STRING,
link: Sequelize.STRING,
source: Sequelize.STRING
});
Dict.hasMany(Entry, {
foreignKey: 'dict_id'
});
Entry.hasMany(Heteronym, {
foreignKey: 'entry_id'
});
Heteronym.hasMany(Definition, {
foreignKey: 'heteronym_id'
});
app.get('/', function(req, res) {
return res.set('Content-Type', 'text/html').send(fs.readFileSync("./index.html"));
});
app.get('/s/:q', function(req, res) {
var q;
res.set('Content-Type', 'text/plain; charset=UTF-8');
if (/^[\u4e00-\u9fa5]+$/.test(q = req.params['q']))
return Entry.findAll({
where: "title like '" + q + "%'"
}).then(function(entries) {
if (entries.length > 1)
entries = [entries[0]];
async.mapSeries(entries, function(entry, callback) {
entry.getHeteronyms().then(function(heteronyms) {
entry.heteronyms = heteronyms;
async.mapSeries(heteronyms, function(heteronym, callback) {
heteronym.getDefinitions().then(function(definitions) {
heteronym.definitions = definitions;
callback(null, heteronym);
});
heteronym = heteronym.dataValues;
}, function(err, heteronyms) {
callback(null, entry);
});
});
entry = entry.dataValues;
}, function(err, results) {
res.send(JSON.stringify(results));
});
});
else
res.send(JSON.stringify([]));
});
app.listen('3000');
}).call(this);