-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
server.js
50 lines (43 loc) · 1.3 KB
/
server.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
var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var Tesseract = require("tesseract.js");
var multer = require("multer");
var app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(require('prerender-node'));
app.use(express.static(__dirname + "/"));
app.set("views", path.join(__dirname, "views"));
app.engine("html", require("ejs").renderFile);
app.set("view engine", "html");
const storage = multer.diskStorage({
destination: (req, res, cb) => {
cb(null, "./uploads");
},
filename: (req, res, cb) => {
cb(null, res.originalname);
},
});
var upload = multer({ storage: storage });
app.get("/", async function (req, res, next) {
res.render("index", { title: "Express" });
});
app.post("/detect", upload.single("avatar"), function (req, res, next) {
const image = path.resolve(__dirname, "./" + req.file.path);
console.log(req.file);
console.log(image);
try {
Tesseract.recognize(image, `eng`, {
logger: (m) => console.log(m),
tessjs_create_pdf: "1",
}).then(({ data: { text } }) => {
console.log(text);
res.send({ result: text });
});
} catch (error) {
res.send({ result: error });
}
});
app.listen(process.env.PORT || 5000);