-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
121 lines (107 loc) · 3.93 KB
/
index.js
File metadata and controls
121 lines (107 loc) · 3.93 KB
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
const fs = require('fs');
const path = require('path');
const https = require('https');
const express = require('express');
const pg = require('pg');
const staticGzip = require('http-static-gzip-regexp');
const cors = require('cors');
const bodyParser = require('body-parser');
const getFileSize = require('remote-file-size');
const app = express();
const device = require('express-device');
const favicon = require('serve-favicon');
const getModel = require('./scripts/getModel.js');
const analyze = require('./scripts/analyze.js');
app.use(device.capture());
app.use(cors());
app.use(bodyParser.urlencoded({
limit: '50mb',
extended: true
}));
app.use(bodyParser.json({
limit: '50mb',
type: 'application/json'
}));
app.use(favicon(path.join(__dirname, 'assets', 'favicon.ico')));
const root = path.join(__dirname, 'assets');
if (process.env.DATABASE_URL) {
const pgConnectionString = process.env.DATABASE_URL;
global.client = new pg.Client(pgConnectionString);
client.connect();
} else if (process.env.MYSQLCONNSTR_localdb) {
const mysqlConnectionString = process.env.MYSQLCONNSTR_localdb;
}
app.use(staticGzip(/(naturalScript\.min\.js|db-manager\.min\.html|loader\.min\.js|loader-CodePenVersion\.min\.js)$/));
app.use(express.static(root));
app.listen(process.env.PORT || 3000);
app.post('/getFileSize', function (req, res) {
getFileSize(req.body.fileURL, function (err, o) {
res.send(String(o));
});
});
app.get('/deviceForm', function (req, res) {
res.send(req.device.type);
});
app.get('/getBuildInfo', function (req, res) {
res.sendFile(path.join(__dirname, 'buildInfo.json'))
});
app.post('/submitCommand', function (req, res) {
if (req.body.type == 'command') {
client.query('INSERT INTO commands(contributer, email, command) VALUES($1, $2, $3)', [req.body.contributer, req.body.email, req.body.command]);
} else if (req.body.type == 'dictionary') {
client.query('INSERT INTO dictionary(contributer, email, db) VALUES($1, $2, $3)', [req.body.contributer, req.body.email, req.body.DB]);
}
res.send('Thanks for helping in shaping Jste :)')
});
app.post('/autoCorrect', function (req, res) {
let key = process.env.BING_SPELL_CHECK_KEY;
let request_params = {
method: 'POST',
hostname: 'api.cognitive.microsoft.com',
path: `/bing/v7.0/spellcheck?mkt=${req.body.lang}&mode=spell`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': req.body.input.length + 5,
'Ocp-Apim-Subscription-Key': key
}
};
let response_handler = function (response) {
let body = '';
response.on('data', function (d) {
body += d;
});
response.on('end', function () {
var corrections = JSON.parse(body).flaggedTokens;
var result = req.body.input;
for (var i = 0; i < corrections.length; i++) {
var result = result.replace(corrections[i].token, corrections[i].suggestions[0].suggestion);
}
res.send(result)
});
response.on('error', function (e) {
res.send('Error: ' + e.message);
});
};
let reqCorrections = https.request(request_params, response_handler);
reqCorrections.write("text=" + req.body.input);
reqCorrections.end();
});
app.post('/getVideoInfo', function (req, res) {
https.get(`https://jste-video-dl.herokuapp.com/api/info?url=${req.body.url}`, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
res.send(JSON.parse(data));
});
}).on("error", (err) => {
res.send("Error: " + err.message);
});
});
app.post('/analyze', function (req, res) {
analyze(req.body.text).then(result => res.send(result))
});
app.get('/getModel', function (req, res) {
getModel(req.body.text).then(model => res.send(model))
});