-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
12e18cb
commit 2eaaf05
Showing
1,219 changed files
with
238,646 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}); |
Oops, something went wrong.