Skip to content

Commit

Permalink
Merge pull request #4 from ogs-at-usi/creation_server
Browse files Browse the repository at this point in the history
Creation server
  • Loading branch information
micheledallerive authored Nov 24, 2022
2 parents 404aa2d + 1c0a304 commit 4892c07
Show file tree
Hide file tree
Showing 8 changed files with 12,179 additions and 0 deletions.
72 changes: 72 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@



//require framework and middleware dependencies
const express = require('express');
const path = require('path');
const logger = require('morgan');
const methodOverride = require('method-override');
const multer = require('multer');

const ejsc = require('ejsc-views');

const fs = require('fs-extra');



//init framework
const app = express();



app.use(logger('dev'));
app.use(express.urlencoded({ extended: false })); // parse application/x-www-form-urlencoded
app.use(express.json({limit: '4MB'})); // parse application/json
app.use(multer().none()); //parse multipart/form-data

app.use(express.static(path.join(__dirname, 'public'), {index: "index.html"}));


app.set('view engine', 'html');

ejsc.compile(views_dir = "views", output_dir = "public/js", details = false);

//co

// TODO - controllers


// TODO - add routes here

//default fallback handlers
// catch 404 and forward to error handler
app.use(function(req, res, next) {
const err = new Error('Not Found');
err.status = 404;
next(err);
});

app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.json({
message: err.message,
error: err
});
});


//start server
app.set('port', process.env.PORT || 8888);

var server = require('http').createServer(app);

server.on('listening', function() {
console.log('Express server listening on port ' + server.address().port);
});

// TODO websocket server

server.listen(app.get('port'));



39 changes: 39 additions & 0 deletions ejs-compile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const fs = require('fs-extra');
const ejs = require('ejs');
const glob = require('glob-fs')({ gitignore: true });

function compile(view) {

//let v = path.basename(view, '.ejs');

let v = view.replace(/\//g,"_").replace(/\.ejs$/,"");

console.log(v);

let template = new String(fs.readFileSync(view));
let f = ejs.compile(template, {client:true});

let f_str = f.toString();

f_str = f_str.replace("function anonymous(","ejs."+v+" = function(").replace("function(locals, escapeFn, include, rethrow", "function(locals, escapeFn, include = ejs.views_include(locals), rethrow")

return f_str;

}

//let compiled = fs.readdirSync("views").filter(f=>f.endsWith(".ejs")).map(f=>`views/${f}`).map(compile).join("\n\n");

let compiled = glob.readdirSync("views/**/*.ejs").map(compile).join("\n\n");

let output = `//EJS Compiled Views - This file was automatically generated on ${new Date()}
ejs.views_include = function(locals) {
console.log("views_include_setup",locals);
return function(path, d) {
console.log("ejs.views_include",path,d);
return ejs["views_"+path.replace(/\\\//g,"_")]({...d,...locals}, null, ejs.views_include(locals));
}
};
${compiled}`

fs.writeFile("public/js/views.js", output);

Loading

0 comments on commit 4892c07

Please sign in to comment.