-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.min.js
157 lines (127 loc) · 4.37 KB
/
app.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// 20092017
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var val = require("validator");
var knex = require('knex')({
dialect: 'sqlite3',
connection: {
filename: './main.db'
}
});
/** inky utils**/
const c = console.log;
const ct = console.trace;
const throwerr = (err) => {throw err;};
// var index = require('./routes/index');
// var users = require('./routes/users');
var app = express();
/** init mongo db **/
var mongoose = require('mongoose');
var mongoDB = 'mongodb://inkyzima:[email protected]:33044/inkyzima';
mongoose.connect(mongoDB);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
// init mongoose models
var SomeModel = require('./models/somemodel.js');
var Author = require('./models/author.js');
var Book = require('./models/book.js');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
/** routes go here **/
app.get('/', function(req, res, next) {
var testdata = new SomeModel({a_string: "testdata string"});
testdata.save((err, data) => {
if (err) return console.error(err);
SomeModel.find().then(data => {
res.render('index', { title: 'new title' , dbtest: data[0]["a_string"]});
});
});
});
app.get('/singlepageapp', function(req, res, next) {
res.render("singlepageapp");
});
app.post("/restaurants/create", (req, res, next) => {
res.cookie("testcookie","testcookie value");
res.render("restaurants_list");
});
// encapsulation to save route-relevant data
(function () {
const template = {restaurantname: val.isEmail, doesdelivery: val.isBoolean};
app.post("/formdata" ,(req,res,next) => {
validate(template,req.body).catch(throwerr()).then(() =>
{
return knex("formdata").insert(req.body);
}).catch(throwerr()).then(() => {
res.send("success")
}).catch((err) => next(err));
});
})();
/** mogoose model example **/
// Use the SomeModel object (model) to find all SomeModel records
//SomeModel.find(callback_function);
/** error handling **/
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
// res.status(err.status || 500);
// res.render('error');
res.status(400).send(err);
});
/** helper functions **/
function validate(template, obj) {
return new Promise ((res,rej) => {
var tkeys = Object.getOwnPropertyNames(template);
var okeys = Object.getOwnPropertyNames(obj);
if (tkeys.length != okeys.length) rej("validated object has a different amount of properties than expected");
for (var i = 0; i < tkeys.length; i++) {
if (okeys.indexOf(tkeys[i]) < 0) rej("validated object has differently named properties than expected");
if (!template[tkeys[i]](obj[tkeys[i]])) rej("validated object failed type validation for property: " + tkeys[i]);
}
res("validate return value");
}); // promise
} // function
function insertintodb (req,res,next) {
}
module.exports = app;
/*
c(req.body)
// here should arrive only form submits. template shuold actually be drawn automatically.. from the jade view.
// if the names of the input fields get changed, they have to be changed here too...
validate(template,req.body)
.then(
// push to db
() => {
// knex("people").insert(req.body).then(null,(err) => {c(err); res.status(500); res.send("db insert failed.")})
// res.send("validation passed");
return knex("formdata").insert(req.body);
},
// throw propagates error while rejecting .then promise
(err) => {throw err;})
.then(
// sql insert successfull
() => {res.send("success")},
(err) => {throw err;})
// finally pass the error to express error handler
.catch((err) => next(err));
*/