-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
131 lines (115 loc) · 3.62 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
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
122
123
124
125
126
127
128
129
130
131
'use strict';
const express = require('express');
const path = require('path');
const fileUpload = require('express-fileupload');
const pdf = require('pdf-parse');
const mammoth = require('mammoth');
const fetch = require('node-fetch');
const articleExtractor = require('article-parser').extract;
const contentExtractor = require('node-article-extractor');
const summarizeText = require('./summarizer');
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.use(fileUpload());
app.use(express.json());
app.post('/document', extractText, (req, res) => {
const text = res.locals.text;
try {
const summary = summarizeText(text);
res.status(200).json({
summary,
});
} catch (error) {
res.status(500).json({
message: 'Something went wrong',
});
}
});
app.post('/article', extractArticle, (req, res) => {
const data = res.locals.data;
try {
const summary = summarizeText(data.text);
data.text = summary;
data.type = 'article';
res.status(200).json(data);
} catch (error) {
res.status(500).json({
message: 'Something went wrong',
});
}
});
app.post('/text', (req, res) => {
try {
const text = req.body.payload;
const summary = summarizeText(text);
res.status(200).json({
summary,
type: 'text',
});
} catch (error) {
res.status(500).json({
message: 'Something went wrong',
});
}
});
async function extractText(req, res, next) {
if (!req.files)
return res.status(400).json({
message: 'Please select a document to summarize',
});
const document = req.files.document;
const extractExtension = () => {
const startIndex = document.name.lastIndexOf('.') + 1;
const extension = document.name.substring(startIndex);
return extension;
};
try {
if (document.mimetype === 'text/plain')
res.locals.text = document.data.toString('UTF-8');
else if (document.mimetype === 'application/pdf') {
const data = await pdf(document.data);
res.locals.text = data.text;
} else if (['doc', 'docx'].includes(extractExtension())) {
const data = await mammoth.extractRawText({
buffer: document.data,
});
res.locals.text = data.value;
} else
return res.status(400).json({
message:
'Incorrect document type, only PDF, DOC, DOCX and TXT document types are supported',
});
} catch (error) {
return res.status(500).json({
message: 'Something went wrong',
});
}
next();
}
async function extractArticle(req, res, next) {
try {
const URL = req.body.payload;
const response = await fetch(URL);
const markup = await response.text();
const article = await articleExtractor(URL);
const content = contentExtractor(markup);
res.locals.data = {
title: article.title,
description: article.description,
author: article.author,
source: article.source,
published: article.published,
text: content.text,
};
next();
} catch (error) {
res.status(500).json({
message: 'Something went wrong',
});
}
}
app.use((req, res) => {
res.sendFile(path.join(__dirname, 'public', '404.html'));
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server Listening on Port ${PORT}`));