Skip to content

Commit

Permalink
add cors
Browse files Browse the repository at this point in the history
  • Loading branch information
abedmurrar committed Dec 23, 2019
1 parent 76ed2fc commit 0baab95
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
38 changes: 26 additions & 12 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,29 @@ const logger = require('morgan');
const session = require('express-session');
const helmet = require('helmet');
const createError = require('http-errors');

/* Routes */
const {wellsRouter, usersRouter} = require('./routes');
const cors = require('cors');

/* Session & Database configuration */
const {Model} = require('objection');
const { Model } = require('objection');
const KnexSessionStore = require('connect-session-knex')(session);
const { wellsRouter, usersRouter } = require('./api');
/* Routes */
const knex = require('./db/config');

Model.knex(knex);

/* Start Express App */
const app = express();
app.set('trust proxy', 1); // trust first proxy

/* Middleware */
app.use(
cors({
origin: true,
credentials: true
})
);

app.use(
helmet({
xssFilter: true,
Expand All @@ -33,33 +41,39 @@ app.use(

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(express.urlencoded({ extended: false }));
app.use(
session({
store: new KnexSessionStore({knex, tablename: 'sessions'}),
store: new KnexSessionStore({ knex, tablename: 'sessions' }),
resave: true,
saveUninitialized: true,
secret: '@@W@@', //TODO: Change it for production
name: ']cUpQwIOA~gLUedE/`u6DHzI"{j$f;',
secret: '@@W@@', // TODO: Change it for production
name: 'k',
cookie: {
signed: true,
maxAge: 30000 // 30 seconds for testing
secure: false,
maxAge:
1000 /* 1000ms */ *
60 /* seconds */ *
60 /* minutes */ *
24 /* hours */ *
365 /* days */
}
})
);
/* Set up Routers */
app.use('/users', usersRouter);
app.use('/wells', wellsRouter);
app.use('/api/users', usersRouter);
app.use('/api/wells', wellsRouter);

/* Catch 404 and forward to error handler */
app.use((req, res, next) => next(createError(404)));

/* Error Handler */
// eslint-disable-next-line no-unused-vars
app.use((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.json(err);
Expand Down
14 changes: 9 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"dependencies": {
"connect-session-knex": "^1.5.0",
"cookie-parser": "~1.4.4",
"cors": "^2.8.5",
"debug": "~2.6.9",
"ejs": "~2.6.1",
"express": "^4.16.4",
"express-session": "^1.17.0",
"express-validator": "^6.2.0",
Expand Down

0 comments on commit 0baab95

Please sign in to comment.