-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
203 lines (158 loc) · 4.9 KB
/
main.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
201
202
203
/**
* $File: main.js $
* $Date: 2018-09-27 16:02:42 $
* $Revision: $
* $Creator: Jen-Chieh Shen $
* $Notice: See LICENSE.txt for modification and distribution information
* Copyright © 2018 by Shen, Jen-Chieh $
*/
"use strict";
const fs = require('fs');
const path = require('path');
/* Include `express.js' */
const express = require('express');
const app = express();
/* `directory-tree' */
const dirTree = require('directory-tree');
/* `self' */
const config = require('./config');
/* Conversion Keywords */
const spaceKey = "_sp_"; // Must be the same as client.
// Setup website directory.
app.use(express.static(config.WEBSITE_DIR));
/* Register all request. */
app.get('/blog_index_data', blog_index_data);
app.get('/search_blog/:search_keyword', search_blog);
// Error handler
app.use(function(err, req, res, next) {
res.status(422).send({ error: err.message });
});;
app.set('port', process.env.PORT || config.PORT);
// Start listening..
const server = app.listen(app.get('port'), function () {
console.log("Server active successfully... Port: " + app.get('port'));
});
//=============== functions ========================//
/**
* Send the scripting reference index data.
*
* @param { typename } req : request handler.
* @param { typename } res : response handler.
* @param { typename } next : error handler.
* @returns { JSON } : tree structure of the API.
*/
function blog_index_data(req, res, next) {
let tree = getBlogTree();
res.send(JSON.stringify(tree));
}
/**
* Search the api files and return the all match result.
*
* @param { typename } req : request handler.
* @param { typename } res : response handler.
* @param { typename } next : error handler.
* @returns { JSON } : Search result.
*/
function search_blog(req, res, next) {
let data = req.params; // get all params
let searchKeyword = data.search_keyword;
/* Conversion base on the rule. */
// This rule must match client side.
searchKeyword = searchKeyword.split(spaceKey).join(" ");
let searchResults = [];
let tree = getBlogTree();
searchMatchPath(tree.children, searchKeyword, searchResults);
res.send(JSON.stringify(searchResults));
}
/* Get the Blog tree. */
function getBlogTree() {
const tree = dirTree(config.BLOG_DIR_PATH, { extensions: config.CONTENT_EXTENSION, normalizePath: true });
// Sort it.
sortTreeByType(tree.children, config.SORT_ORDER);
// Modefied the `BLOG_DIR_PATH' to the correct format string.
var removePath = config.BLOG_DIR_PATH;
removePath = removePath.replace("./", "");
removeLeadPath(tree.children, removePath);
return tree;
}
/**
* Remove the `BLOG_DIR_PATH', so when the client
* receive the data would not need to care where is the api directory located.
* @param { JSON } dir : directory JSON object.
* @param { typename } rmPath : Param desc here..
*/
function removeLeadPath(dir, rmPath) {
for (let index = 0;
index < dir.length;
++index)
{
let pathObj = dir[index];
if (pathObj.children != null && pathObj.children.length != 0) {
removeLeadPath(pathObj.children, rmPath);
}
// Remove the `MANUAL_DIR_PATH' or `BLOG_DIR_PATH' path.
pathObj.path = pathObj.path.replace(rmPath, "");
}
}
/**
* Search the match result.
* @param { JSON } dir : directory JSON object.
* @param { string } match : stirng check to match.
* @param { array } arr : Array to store search result.
*/
function searchMatchPath(dir, match, arr) {
for (let index = 0;
index < dir.length;
++index)
{
let pathObj = dir[index];
if (pathObj.children != null && pathObj.children.length != 0) {
searchMatchPath(pathObj.children, match, arr);
}
/* Make it case insensitive. */
let nameUpper = pathObj.name.toUpperCase();
let matchUpper = match.toUpperCase();
// If match add it to search result.
if (nameUpper.includes(matchUpper) && pathObj.type == 'file') {
arr.push(pathObj);
}
}
}
/**
* Sort the tree result by directory/file type.
* @param { tree.children } tree : Tree children.
* @param { string } type : Sort type, enter 'directory' or 'file'.
*/
function sortTreeByType(tree, type = 'directory') {
if (type != 'directory' && type != 'file')
return;
let tarList = []; // target list.
let anoList = []; // another list.
/* Split path object into two arrays by type. */
{
for (let index = 0;
index < tree.length;
++index)
{
let pathObj = tree[index];
if (pathObj.children != null && pathObj.children.length != 0) {
sortTreeByType(pathObj.children, type);
}
// Add path object by type.
if (pathObj.type == type)
tarList.push(pathObj);
else
anoList.push(pathObj);
}
}
/* Copy array over. */
{
let resultTree = tarList.concat(anoList);
for (let index = 0;
index < tree.length;
++index)
{
tree[index] = resultTree[index];
}
}
}