-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (66 loc) · 1.97 KB
/
index.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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const morgan = require('morgan');
const multer = require('multer');
const createWorker = require('tesseract.js');
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cors());
const PORT = process.env.PORT || 5000
const storage = multer.diskStorage({
destination: (req, file, callBack) => {
callBack(null, 'uploads')
} ,
filename: (Req, file, callBack) => {
callBack(null, `${file.originalname}`);
}
});
var upload = multer({storage: storage});
// var upload = multer({dest: 'uploads/'});
function test() {
}
app.post('/file', upload.single('file'), (req, res, next) => {
const file = req.file;
console.log(file.filename);
var text = test();
if (!file) {
const error = new Error('Please upload the file');
error.statusCode = 400;
return next(error);
}
// ocr
const worker = createWorker.createWorker({
logger: m => console.log((m.progress)*100 + "%"), // Add logger here
});
(async () => {
await worker.load();
await worker.loadLanguage('eng');
await worker.initialize('eng');
const { data: { text } } = await worker.recognize(`./uploads/${file.originalname}`);
console.log(text);
await worker.terminate();
await res.json({
text: text,
});
})();
});
//multiple files
app.post('/multipleFiles', upload.array('files'), (req, res, next) => {
const files = req.files;
console.log(files);
if (!files) {
const error = new Error('Please upload the file');
error.statusCode = 400;
return next(error);
}
res.send({status: 'ok'});
});
app.get('/test', (req, res) => {
res.status(200).json({
message: 'test work!'
});
});
app.use(morgan());
app.listen(PORT, () => console.log(`server running on ${PORT} ...`));