Skip to content

Commit

Permalink
1) Changed UI
Browse files Browse the repository at this point in the history
2) Created commenting sys - WIP
3) Added search page
4) Socket.io and FS support
5) will add all of the Irish back soon
  • Loading branch information
Low-Fat-Lard committed Oct 2, 2022
1 parent 12e18cb commit 2eaaf05
Show file tree
Hide file tree
Showing 1,219 changed files with 238,646 additions and 96 deletions.
8 changes: 8 additions & 0 deletions .breakpoints
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
{
"files": {
"views/index.ejs": [
{
"id": "45d3f9e0-0855-4f49-9d54-00011c145d35",
"line": 16,
"version": 552,
"index": 453
}
],
"views/post.ejs": [
{
"id": "728f013b-b0b8-42e7-afa4-46729eb5cb5d",
Expand Down
12 changes: 10 additions & 2 deletions database.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
{

[
{
"title": ["Hello world"],
"posturl": ["test"],
"content": ["My first blog post"]
}
},
{
"title": ["Hello world2"],
"posturl": ["test2"],
"content": ["My first blog post2"]
}
]
101 changes: 73 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,86 @@
// Declare express
const express = require('express');
const express = require('express')
const app = express()
const http = require('http');

// Declare database and requitment
const JSONdb = require('simple-json-db');
const db = new JSONdb('database.json');
// Import socket.io
const socketIo = require('socket.io');
const server = http.Server(app);

// Declare port
const app = express();
const port = 4040;
// restart
const io = socketIo(server);
const path = require("path");
const { writeFileSync, readFileSync } = require('fs');
var data;
let loadPosts = () => JSON.parse(readFileSync('database.json'));
let loadReplies = () => JSON.parse(readFileSync('replies.json'));
var replies = { id: 1, body: '3000', time: '12:00' };
var config = [];
const d = new Date();
const dateFormat = d.toDateString();
var writectx = null;

// Set view engine
app.set('view engine', 'ejs');

// Set main page
app.get('/', (req, res) => {
app.locals.title = db.get('title');
app.locals.postUrl = db.get('posturl');
io.on('connection', function connection(ws) {
console.log('A new client Connected!');
ws.send('Welcome New Client!');

res.render('index');
})
ws.on("reply", function incoming(message) {
console.log('received: %s', message);

// Set blog page
app.get('/post/:posturl', (req, res) => {
postUrl = req.params.posturl;
dbIndex = db.get('posturl').indexOf(postUrl);
var replyArray = message.toString();
var reply = replyArray.split(",");

replies = { id: parseInt(reply[0], 10), body: reply[1], time: dateFormat };
writectx = message;
const path = './replies.json';
config.push(replies);
try {
writeFileSync(path, JSON.stringify(config, null, 2), 'utf8');
console.log('Data successfully saved to disk');
} catch (error) {
console.log('An error has occurred ', error);
}

});
});

server.listen(3000, () => console.log(`Lisening on port :3000`))

if (dbIndex != -1) {
app.locals.title = db.get('title')[dbIndex];
app.locals.content = db.get('content')[dbIndex];

res.render('post');
app.use(express.static(__dirname + "/view"));
app.use(express.static(__dirname + '/public'));
//main page
app.get("/", function(request, response) {
response.render(path.join(__dirname + "/view/index.ejs"));
})
//json
app.get("/posts", function(request, response) {
response.send(loadPosts());
});
//search
app.get("/search/:searchterm", function(request, response) {
app.locals.term = request.params.searchterm;
response.render(path.join(__dirname + "/view/search.ejs"));
});
//post
app.get('/post/:posturl', (request, response) => {
postUrl = request.params.posturl;
for (var i = 0; i < loadPosts().length; i++) {
dbIndex = JSON.stringify(loadPosts()[i]).indexOf(postUrl);
if (loadPosts()[i].posturl == postUrl) {
app.locals.title = loadPosts()[i].title;
app.locals.content = loadPosts()[i].content;
app.locals.posturl = loadPosts()[i].posturl;
}
}
if (dbIndex != -1) {
response.render(path.join(__dirname + "/view/post.ejs"));
} else {
res.send('Page not found :(')
//error
response.send('Page not found :(')
}
});

// Run app
app.listen(port, () => {
console.log('App is live');
app.get("/replies", function(request, response) {
response.send(loadReplies());
});
Loading

0 comments on commit 2eaaf05

Please sign in to comment.