Files
blogger-nodejs/src/server.js
2025-12-19 17:31:28 +00:00

90 lines
3.7 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")
config = require('../config.json');
// 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);
function perform_checks()
{
console.log("Performing startup checks...")
exit_flag = false
required_values = ['site_admin','seperator','site_name','site_url','locale','port','cache_data','allow_signup','site_description','request_data_limit','enable_hitcount','charset','root_path','edit_account_base_url','new_post_url','signup_url','default_commenter_username','rss','atom','date_format','time_zone','css']
// Perform some standard checks:
// data_storage
switch (config.data_storage)
{
case 'mysql':
case 'json':
break
default:
console.log("[ ERROR ] invalid value in `data_storage`\nPlease modify config.json. Value should be 'mysql' or 'json'.")
exit_flag = true
}
// auto_generated
if (config.auto_generated)
{
console.log("[ ERROR ] `autogenerated` option set to true\nplease edit the config.json file to include your relevant information, then set to false or remove the autogenerated option.")
exit_flag = true
}
// Check required values are present
required_values.forEach((value) => { // Use a loop to check each required value is present
if (typeof config[value] == 'undefined') {
exit_flag = true
console.log(`[ ERROR ] \`${value}\` is undefined\nPlease set it to something, read the documentation for help.`)
}
});
if (exit_flag)
{
console.log("Exiting due to errors.")
process.exit(1)
}
}
perform_checks()
app.listen(config.port, () => {
console.log(`Server is running at http://localhost:${config.port} webroot: ${config.root_path}`);
console.log("Running in: ", __dirname)
});