nodejs-website/main.js

82 lines
1.9 KiB
JavaScript
Raw Normal View History

2022-10-06 07:05:46 -05:00
#!/usr/bin/node
'use strict';
/*
2022-09-14 Mockup of what a web server may look like.
Should serve very quick since it's all from scratch, nothing to load like WordPress.
May even skip out on things like
*/
const app_name = "www.example.com";
let express = require('express');
let app = express();
const execSync = require('child_process').execSync;
let pages = {};
pages.home = require('./pages/home.js');
//pages.notice = require('./pages/notice.js');
let pages_php = {};
pages.home = './pages/home.php';
//pages.notice = './pages/notice.php';
// Even better, look through ./pages/ for *.php or *.js and add them to the array!
let ports = [];
ports.push(8080);
async function main() {
console.log("Starting Main");
2022-10-06 22:15:36 -05:00
app.use(function (req, res, next) {
res.contentType('text/html');
next();
});
2022-10-06 07:05:46 -05:00
console.log("Adding Routes");
2022-10-06 22:15:36 -05:00
let router = express.Router();
2022-10-06 07:05:46 -05:00
/* MANUAL METHOD, SPECIFY EVERY PATH+FILE
2022-10-06 22:15:36 -05:00
router.get('/', function (req, res) {
console.log("Fetching");
require('lib/home.js'); // one way, maybe?
});
router.get('/notice', function (req, res) {
pages.notice(req, res); // another way?
});
2022-10-06 07:05:46 -05:00
*/
/* AUTOMATIC METHOD BASED ON OBJECT/ARRAY
2022-10-06 22:15:36 -05:00
for page in pages { // FORTESTING pseudocode
router.get("/" + page.key, function (req,res) {
page.value(req, res);
});
}
2022-10-06 07:05:46 -05:00
*/
/* AUTOMATIC METHOD BASED ON OBJECT/ARRAY OF PHP scripts
2022-10-06 22:15:36 -05:00
for page in pages { // FORTESTING pseudocode
router.get("/" + page.key, function (req,res) {
res.send(system("php page.value"));
});
}
2022-10-06 07:05:46 -05:00
*/
2022-10-06 22:15:36 -05:00
// Test (Also a decent catch-all to Home!)
router.get('/*', function (req, res) {
//console.log(req);
//console.log(res);
console.log(req.socket.remoteAddress, "asked for", req.url)
let html = execSync("php ./pages/home.php");
res.send(html);
});
2022-10-06 07:05:46 -05:00
2022-10-06 22:15:36 -05:00
app.use('', router);
2022-10-06 07:05:46 -05:00
console.log("Adding Ports");
2022-10-06 22:15:36 -05:00
ports.forEach(port => {
app.listen(port);
console.log(" * Now listening on port " + port + ".");
});
2022-10-06 07:05:46 -05:00
console.log("Done! Now we wait...");
}
main();