const express = require('express'); const crypto = require('crypto'); const fs = require('fs'); const users = require('./users.js'); const posts = require('./posts.js'); const config = require('./config.js'); const app = express(); const port = 8005; app.use(express.urlencoded({ extended: true })); app.use(express.json()); app.use(express.static(config.root_path)); function unix_time_to_date_format(unix_time) { let date_object = new Date(unix_time) let formatter = new Intl.DateTimeFormat(config.date_format,config.date_options); let formatted_time = formatter.format(date_object); return formatted_time } function replace_format_indicators(input_string, post_index) { post_object = posts.posts[post_index] output_string = input_string .replace("%A", (post_object["tags"])) .replace("%C", post_object["content"]) .replace("%D", unix_time_to_date_format(post_object["pubdate"])) .replace("%E", unix_time_to_date_format(post_object["editdate"])) .replace("%L", `/post/${post_index}`) .replace("%N", post_object["poster"]) .replace("%P", "/post") .replace("%O", "/edit") .replace("%R", "/rss") .replace("%S", config.seperator) .replace("%T", post_object["title"]) .replace("%U", `/user/${post_object["poster"]}`) .replace("%Y", config.site_name) .replace("%W", config.site_description) return output_string } app.get("/", (req,res) => { header_div = config.timeline_header header_div = replace_format_indicators(header_div, 0); posts_div = ""; counter = posts.posts.length - 1; while ((counter >= 0) && (counter > (posts.posts.length - (config.timeline_length + 1)))) { let post = config.timeline_post_format; posts_div += replace_format_indicators(post, counter); counter -= 1; } res.send(`
${posts_div}
`); }); app.get("/post", (req,res) => { res.send(`





`); }); app.get("/edit", (req,res) => { res.send(`Edit page under construction`); }); app.get("/user/:username", (req, res) => { header_div = `

${req.params.username}

` posts_div = ""; for (let post_index = posts.posts.length-1; post_index >= 0; post_index--) { if (posts.posts[post_index]["poster"] == req.params.username) { let post = config.user_post_format; posts_div += replace_format_indicators(post, post_index); } } res.send(`
${posts_div}
`); }); app.get("/post/:post_index", (req, res) => { post_div = ""; let post = config.post_page_format; post_div += replace_format_indicators(post, req.params.post_index); res.send(`
${post_div}
`); }); app.post("/submit_post", (req,res) => { const password = crypto.createHash('sha512').update(req.body.password).digest('hex'); const username = req.body.username const title = req.body.title const content = req.body.content const tags = req.body.tags.split(','); const datetime = new Date().getTime() console.log(username, "is submitting a post titled:", title); if (users.users[username] == password) { // Password matches posts.posts.push({ "poster": username, "title": title, "content": content, "pubdate": datetime, "editdate": datetime, "tags": tags, }) fs.writeFileSync(`${config.root_path}/posts.js`, `export const posts = ${JSON.stringify(posts.posts)}`, 'utf-8'); res.redirect(302, "/"); } else { res.send(`Invalid Password for user`,username); } }); app.listen(port, () => { console.log(`Server is running at http://localhost:${port}`); });