Wrote the news rendering module

This commit is contained in:
2019-05-22 06:53:25 +10:00
parent 2298cb7e16
commit 12016a3fbf
9 changed files with 439 additions and 16 deletions
+4
View File
@@ -10,6 +10,10 @@ let path = require('path');
app.use(bodyParser.json());
//handle the news request
let news = require('./news.js');
app.post('/newsrequest', news.newsRequest());
//database
let { connectToDatabase } = require('./database.js');
let connection = connectToDatabase(); //uses .env
+33
View File
@@ -0,0 +1,33 @@
//environment variables
require('dotenv').config();
//libraries
let fs = require('fs');
let path = require('path');
const newsRequest = () => (req, res) => {
let fpath = path.join(__dirname, '..', 'public', 'news');
let fileNames = fs.readdirSync(fpath);
//set the maximum
let max = parseInt(req.body.max);
if (max > fileNames.length) {
max = fileNames.length;
}
//build the object to send
let json = {}
//send each file as json
for (let i = 0; i < max; i++) {
json[fileNames[fileNames.length - i - 1]] = fs.readFileSync(fpath + '\\' + fileNames[fileNames.length - i - 1], 'utf8');
}
//actually send the data
res.json(json);
res.end();
}
module.exports = {
newsRequest: newsRequest
}