Repository navigation
Expand file tree
/
Copy pathnode.js
More file actions
52 lines (49 loc) · 1.82 KB
/
Copy pathnode.js
File metadata and controls
52 lines (49 loc) · 1.82 KB
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
'use strict';
const express = require('express');
const app = express();
const port = 3000;
const sqlite3 = require('sqlite3').verbose();
let db = new sqlite3.Database('news.db', sqlite3.OPEN_READONLY, (err) => {
if (err) {
return console.error(err.message);
};
console.log('Connected to database');
});
app.get('/', (req, res) => {
let html = "<div class='text'> ";
db.all('SELECT news.id, news.img_src, news.href, news.title FROM news INNER JOIN articles ON news.id = articles.id WHERE articles.text != "null"', (err, articles) => {
// тук вече имаме данните
for (let i = 0; i < articles.length; i++) {
html += '<p><a href="' + articles[i].id + '">' + articles[i].title + '</a><img src=' + articles[i].img_src + ' width="200" height="100"><br>';
};
html += "</div>"
});
db.all('SELECT news.id, news.img_src, news.href, news.title FROM news INNER JOIN articles ON news.id = articles.id WHERE articles.text == "null"',(err,articles)=>{
html += "<div class='textless'> "
for (let i = 0; i < articles.length; i++) {
html += '<p><a href="' + articles[i].id + '">' + articles[i].title + '</a><img src=' + articles[i].img_src + ' width="200" height="100"><br>';
};
html += "</div>"
res.send(html);
});
});
app.get('/:articleid', (req, res) => {
let html = '<span style="white-space: pre-line">';
db.all('SELECT text FROM articles WHERE id = ?', req.params['articleid'], (err, text) => {
try {
if (text[0].text) {
html += text[0].text;
};
}
catch (e) {
html += "There's nothing here";
};
text += '</span>';
res.send(html);
});
console.log(req.params['articleid']);
});
app.get('/favicon.ico', (req, res) => res.status(204));
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});