-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
85 lines (61 loc) · 2.45 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
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
//declare the express
var express = require('express');
var app = express();
//declare path
var path = require('path');
//declare multer and set a test destination folder
var multer = require('multer');
var upload = multer({dest:'upload/'});
//declare pdf2json api.
var fs = require('fs'),
PDFParser = require("pdf2json");
//initialize pdf2json parser to grab pdf text content as raw
var pdfParser = new PDFParser(this, 1);
//depending on what comes back from pdfParser execute different functions
//initialize an array of someArray
var textArray = ["hello"];
//declares a public folder to be static
//to use path to a folder we require Node module path
//-> exposes a join method allows us to chain variables to create
// a file path: (param of path.join(@first, @second))
// @first : native Node variable "__dirname" contains the file path
// of the current folder.
// @second: name of the folder containing the static recources
app.use('/', express.static(path.join(__dirname, 'public')));
//get is a HTTP method route
//Routes HTTP GET requests to the specific path with the
//specified callback functions.:
// app.get(@first, @second)
// @first: path
// @second: callback function/ function
app.get('/array', function(req, res){
res.send(textArray);
})
//post is a HTTP method route
//Routes HTTP POST requrests a specified path with the specified
//callback functions. Since Express 4 does not support res.files anymore
//another way to grab files is required using "multer":
// app.post(@first, @second, @third):
// @first: path
// @second: uses multer that will save one file into a subdirectory of
// upload, expecting a "pdfFile" name from front-end.
// @third: callback function
app.post('/upload', upload.single("pdfFile"), function(req, res, next){
var file = req.file;
if(file == undefined){
res.status(404).send("FILE NOT FOUND");
}
//load file path into a pdfParser
pdfParser.loadPDF(file.path);
//envoke once then delete the event so that there won't be double callbacks.
pdfParser.once("pdfParser_dataError", errData => console.error(errData.parserError));
pdfParser.once("pdfParser_dataReady", pdfData => {
textArray = pdfParser.getRawTextContent().split(/\s+/);
res.redirect('/home.html');
});
});
//this is used to listen to port 3000 and post a "message" on console when
//server.js (this file) is ran.
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
});