-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathindex.js
More file actions
34 lines (30 loc) · 854 Bytes
/
index.js
File metadata and controls
34 lines (30 loc) · 854 Bytes
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
const e = require('express')
const m = require('multer')
const s = require('child_process').spawn;
const a = e()
const p = 8080
const upload = m({
dest : '/tmp/',
})
a.use(e.static('public'))
const runPy = file => new Promise(function(resolve, reject) {
const p = s('python', ['-W', 'ignore', 'main.py', '--image', `${file}`]);
let buf = '';
p.stdout.on('data', data => {
buf += data
});
p.stdout.on('end', data => {
resolve(buf);
});
p.stderr.on('data', (data) => {
reject(data);
});
});
a.post('/cnh.json', upload.single('file'), (r, a, e) => {
if (!r.file) return a.status(400).json({ Error: 'Something went wrong'})
let file = r.file.path
runPy(file)
.then(d => a.json(`${d}`))
.catch(d => a.status(400).json({ Error: `Something went wrong - ${d}`}))
})
a.listen(p, _ => console.log(`Running on ${p}`))