-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (31 loc) · 1.12 KB
/
index.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
const express = require('express');
const fs = require('fs');
let imagesFile = 'images.json';
let messagesFile = 'messages.json';
// Read it once and then watch for changes afterwards.
let images = JSON.parse(fs.readFileSync(imagesFile, 'utf8'));
let messages = JSON.parse(fs.readFileSync(messagesFile, 'utf8'));
fs.watchFile(imagesFile, function(curr, prev) {
images = JSON.parse(fs.readFileSync(imagesFile, 'utf8'));
});
fs.watchFile(messagesFile, function(curr, prev) {
images = JSON.parse(fs.readFileSync(messagesFile, 'utf8'));
});
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
const app = express();
app.get('/', function(req, res) {
res.status(200).send(":)");
});
app.get('/message', function(req, res) {
const randomMessage = messages[getRandomInt(messages.length)]
res.status(200).send(JSON.stringify(randomMessage))
});
app.get('/image', function(req, res) {
const randomImage = images[getRandomInt(images.length)]
res.status(200).send(JSON.stringify(randomImage))
});
var server = app.listen(8000, function () {
console.log("Affrmitive: ", server.address().port);
});