Files
blogger-nodejs/src/server.js

74 lines
2.8 KiB
JavaScript

// Get the libraries
const fs = require('fs'); // For modifying and reading files
const express = require('express'); // For running a webserver in nodejs
const crypto = require('crypto'); // For encrypting passwords, I use sha512
// fromUnixTime(): Create a date from a Unix timestamp (in seconds). Decimal values will be discarded.
// format(): Return the formatted date string in the given format. The result may vary by locale.
// getUnixTime(): Get the seconds timestamp of the given date.
// find out more at https://date-fns.org/ \or docs: https://date-fns.org/docs/Getting-Started
const { fromUnixTime, format, getUnixTime } = require("date-fns") // A date utility library
const ejs = require("ejs")
const func = require("./functions.js")
const data = require("./data.js")
// Define the modules now so they are global
let users // contains a list of users, each user is an object containing username,prettyname,hash and description
let posts // contains a list of posts,
let comments // contains a list of comments
let config // contains a set of configuration for the site, see example-config.js for an example
config = require('../config.json');
if (config["data_storage"] == "json") {
try {
// We're going to try and import the modules,
users = require('../data/users.json');
posts = require('../data/posts.json');
comments = require('../data/comments.json');
}
catch (error) {
// if they don't all import then
// inform the user to pass --first-time and exit with an error code
console.log("A file is missing!")
console.log("Run 'make' to initialise the data files")
console.log(error)
process.exit(1)
}
}
// Import the locale
try {
locale = require(`../locales/${config.locale}.json`);
}
catch (error) {
console.log("This locale doesn't exist, if you want to create it then you can create a PR")
console.log("Locale selected: ", config.locale)
}
// Define stuff to do with express (nodejs webserver)
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static(config.root_path));
// set the view engine to ejs
app.set('view engine', 'ejs');
app.set('views', '../views')
// Express JS routes
const syndication_routes = require('./routes/syndication.js');
const indexes_routes = require('./routes/indexes.js');
const standard_pages_routes = require('./routes/standard_pages.js');
const forms_routes = require('./routes/forms.js');
const form_actions_routes = require('./routes/form_actions.js');
app.use('/', syndication_routes);
app.use('/', indexes_routes);
app.use('/', standard_pages_routes);
app.use('/', forms_routes);
app.use('/', form_actions_routes);
app.listen(config.port, () => {
console.log(`Server is running at http://localhost:${config.port} webroot: ${config.root_path}`);
console.log("Running in: ", __dirname)
});