Skip to content

Commit

Permalink
added starter
Browse files Browse the repository at this point in the history
  • Loading branch information
lemlooma committed Jun 28, 2021
1 parent 2294913 commit 27b9bfd
Show file tree
Hide file tree
Showing 21 changed files with 3,318 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
57 changes: 57 additions & 0 deletions app.js
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;
7 changes: 7 additions & 0 deletions bin/www
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}...`));
14 changes: 14 additions & 0 deletions config/database.js
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',
},
};
10 changes: 10 additions & 0 deletions config/index.js
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 added db/.DS_Store
Binary file not shown.
Empty file added db/migrations/.keep
Empty file.
37 changes: 37 additions & 0 deletions db/models/index.js
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 added db/seeders/.keep
Empty file.
Loading

0 comments on commit 27b9bfd

Please sign in to comment.