-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
3,318 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const createError = require('http-errors'); | ||
const express = require('express'); | ||
const path = require('path'); | ||
const cookieParser = require('cookie-parser'); | ||
const logger = require('morgan'); | ||
const { sequelize } = require('./db/models'); | ||
const session = require('express-session'); | ||
const SequelizeStore = require('connect-session-sequelize')(session.Store); | ||
const indexRouter = require('./routes/index'); | ||
const usersRouter = require('./routes/users'); | ||
|
||
const app = express(); | ||
|
||
// view engine setup | ||
app.set('view engine', 'pug'); | ||
|
||
app.use(logger('dev')); | ||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: false })); | ||
app.use(cookieParser()); | ||
app.use(express.static(path.join(__dirname, 'public'))); | ||
|
||
// set up session middleware | ||
const store = new SequelizeStore({ db: sequelize }); | ||
|
||
app.use( | ||
session({ | ||
secret: 'superSecret', | ||
store, | ||
saveUninitialized: false, | ||
resave: false, | ||
}) | ||
); | ||
|
||
// create Session table if it doesn't already exist | ||
store.sync(); | ||
|
||
app.use('/', indexRouter); | ||
app.use('/users', usersRouter); | ||
|
||
// catch 404 and forward to error handler | ||
app.use(function (req, res, next) { | ||
next(createError(404)); | ||
}); | ||
|
||
// 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'); | ||
}); | ||
|
||
module.exports = app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env node | ||
|
||
const port = process.env.PORT || '8080'; | ||
|
||
const app = require('../app'); | ||
|
||
app.listen(port, () => console.log(`Listening on port ${port}...`)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const { | ||
db: { username, password, database, host }, | ||
} = require('./index'); | ||
|
||
module.exports = { | ||
development: { | ||
username, | ||
password, | ||
database, | ||
host, | ||
dialect: 'postgres', | ||
seederStorage: 'sequelize', | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module.exports = { | ||
environment: process.env.NODE_ENV || 'development', | ||
port: process.env.PORT || 8080, | ||
db: { | ||
username: process.env.DB_USERNAME, | ||
password: process.env.DB_PASSWORD, | ||
database: process.env.DB_DATABASE, | ||
host: process.env.DB_HOST, | ||
}, | ||
}; |
Binary file not shown.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const Sequelize = require('sequelize'); | ||
const basename = path.basename(__filename); | ||
const env = process.env.NODE_ENV || 'development'; | ||
const config = require(__dirname + '/../../config/database.js')[env]; | ||
const db = {}; | ||
|
||
let sequelize; | ||
if (config.use_env_variable) { | ||
sequelize = new Sequelize(process.env[config.use_env_variable], config); | ||
} else { | ||
sequelize = new Sequelize(config.database, config.username, config.password, config); | ||
} | ||
|
||
fs | ||
.readdirSync(__dirname) | ||
.filter(file => { | ||
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js'); | ||
}) | ||
.forEach(file => { | ||
const model = sequelize['import'](path.join(__dirname, file)); | ||
db[model.name] = model; | ||
}); | ||
|
||
Object.keys(db).forEach(modelName => { | ||
if (db[modelName].associate) { | ||
db[modelName].associate(db); | ||
} | ||
}); | ||
|
||
db.sequelize = sequelize; | ||
db.Sequelize = Sequelize; | ||
|
||
module.exports = db; |
Empty file.
Oops, something went wrong.