-
Notifications
You must be signed in to change notification settings - Fork 112
/
index.js
116 lines (89 loc) · 3.55 KB
/
index.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
'use strict';
const express = require('express');
const exphbs = require('express-handlebars');
const bodyParser = require('body-parser');
const Categories = require('./routes/categories');
const CategoriesAPI = require('./api/categories-api');
const Products = require('./routes/products');
const ProductsAPI = require('./api/products-api');
const UserAPI = require('./api/user-api');
const app = express();
const session = require('express-session');
const flash = require('express-flash');
const CategoryService = require('./services/category-service');
const ProductService = require('./services/product-service');
const UserService = require('./services/user-service');
const pgp = require('pg-promise')();
const DATABASE_URL= process.env.DATABASE_URL || "postgresql://localhost:5432/my_products_list";
const config = {
connectionString : DATABASE_URL
}
if (process.env.NODE_ENV == 'production') {
config.ssl = {
rejectUnauthorized : false
}
}
const db = pgp(config);
// let useSSL = false;
// let local = process.env.LOCAL || false;
// if (process.env.DATABASE_URL && !local){
// useSSL = true;
// }
// const connectionString = process.env.DATABASE_URL || 'postgresql://localhost:5432/my_products_list';
// const db = pgp(connectionString);
const categoryService = CategoryService(db);
const productService = ProductService(db);
const userService = UserService(db);
const categoryRoutes = Categories(categoryService);
const productRoutes = Products(productService, categoryService);
const categoryAPI = CategoriesAPI(categoryService);
const productsAPI = ProductsAPI(productService);
const userAPI = UserAPI(userService);
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true
}));
app.use(flash());
//setup template handlebars as the template engine
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
app.use(express.static(__dirname + '/public'));
//setup middleware
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
function errorHandler(err, req, res, next) {
res.status(500);
res.render('error', { error: err });
}
//setup the handlers
app.get('/categories', categoryRoutes.show);
app.get('/categories/add', categoryRoutes.showAdd);
app.get('/categories/edit/:id', categoryRoutes.get);
app.post('/categories/update/:id', categoryRoutes.update);
app.post('/categories/add', categoryRoutes.add);
//this should be a post but this is only an illustration of CRUD - not on good practices
app.get('/categories/delete/:id', categoryRoutes.delete);
app.get('/', productRoutes.show);
app.get('/products', productRoutes.show);
app.get('/products/edit/:id', productRoutes.get);
app.post('/products/update/:id', productRoutes.update);
app.get('/products/add', productRoutes.showAdd);
app.post('/products/add', productRoutes.add);
//this should be a post but this is only an illustration of CRUD - not on good practices
app.get('/products/delete/:id', productRoutes.delete);
app.get('/api/products', productsAPI.all);
app.post('/api/products', productsAPI.add);
app.get('/api/categories', categoryAPI.all);
app.get('/api/users', userAPI.user);
app.post('/api/signUp', userAPI.signUp);
app.post('/api/login', userAPI.login);
app.use(errorHandler);
//configure the port number using and environment number
var portNumber = process.env.PORT || 3000;
//start everything up
app.listen(portNumber, function () {
console.log('Create, Read, Update, and Delete (CRUD) example server listening on:', portNumber);
});