-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·153 lines (116 loc) · 3.91 KB
/
app.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
// http://localhost:3000/
import "dotenv/config"
console.log("process.env.POSTGRES_HOST: ", process.env.POSTGRES_HOST)
//var createError = require('http-errors');
import createError from "http-errors";
//var express = require('express');
import express from "express";
//var path = require('path');
import path from "path";
//var cookieParser = require('cookie-parser');
import cookieParser from "cookie-parser";
//var logger = require('morgan');
import logger from "morgan";
//var indexRouter = require('./routes/index');
import indexRouter from "./routes/index.js";
//var usersRouter = require('./routes/users');
import usersRouter from "./routes/users.js";
let app = express();
export default app
// for : __dirname
///import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename); // !!! https://flaviocopes.com/fix-dirname-not-defined-es-module-scope/
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
// https://expressjs.com/en/resources/middleware/cookie-session.html
// https://github.com/senchalabs/connect#middleware
// after cookieParser() middleware !!!
// store session state in browser cookie
// shell: npm install cookie-session
//// var cookieSession = require('cookie-session'); // using ESM not CJS modules
import cookieSession from "cookie-session"; // ES modules
app.use(cookieSession({
keys: ['secret1', 'secret2'] // change for security and hide in secrets.js etc.
}));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function (request, response, next) {
next(createError(404));
});
// error handler
app.use(function (err, req, res) {
// set locals, only providing error in development
res.locals.message = err.message;
//res.locals.error = req.app.get('env') === 'development' ? err : {};
res.locals.error = err
// render the error page
res.status(err.status || 500);
res.render('error');
});
//====================================================
//import { pg_passwd } from "./secrets.js"
var mysql = {}
mysql.client = 'mysql2'
mysql.user = "username"
mysql.password = "password"
mysql.database = "express_backend"
var postgresql = {}
postgresql.client = "pg"
postgresql.version = "8.11"
postgresql.user = "postgres"
postgresql.password = "postgres"
postgresql.database = "express_backend"
var db = mysql // mysql or postgresql
import knex_module from "knex"
const knex = knex_module({
client: 'pg',
connection: {
host: process.env.POSTGRES_HOST,
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DATABASE,
},
})
/*
const knex = knex_module({
client: db.client, version: db.version,
connection: {
host: "127.0.0.1",
user: db.user,
password: db.password,
database: db.database
}
})
*/
app.set("knex", knex)
await knex.schema.hasTable("chat_messages").then(function (exists) {
if (exists) return
return knex.schema.createTable('chat_messages', function (table) {
table.increments('id').primary()
table.string('message_text').notNullable()
table.string('sender').notNullable()
table.timestamp('creation_timestamp').defaultTo(knex.fn.now())
})
})
await knex.schema.hasTable("product").then(function (exists) {
if (exists) return
return knex.schema.createTable('product', function (table) {
table.increments('id').primary()
table.string('name').notNullable()
table.string('desc').notNullable()
table.decimal('price', 10, 2).notNullable()
table.timestamp('time').defaultTo(knex.fn.now())
})
})
// http://localhost:3000/
//====================================================
//export default app;