-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
created structure of the server side adding folders and app.js
- Loading branch information
There are no files selected for viewing
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')); | ||
|
||
|
||
|
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); | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.