Last active
June 21, 2017 04:57
-
-
Save zellwk/13c8801c942eca80b499815572bd419f to your computer and use it in GitHub Desktop.
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
const express = require('express') | |
const bodyParser = require('body-parser') | |
const expressValidator = require('express-validator') | |
const routes = require('./routes') | |
const middlewares = require('./middlewares') | |
const errorHandlers = require('./middlewares/errorHandlers') | |
const isProduction = process.env.NODE_ENV === 'production' | |
const passport = require('passport') | |
const app = express() | |
require('./handlers/passport') | |
// ====================================== | |
// # Middlewares | |
// ====================================== | |
app.use(bodyParser.json()) | |
app.use(bodyParser.urlencoded({extended: true})) | |
app.use(expressValidator()) | |
app.use(passport.initialize()) | |
app.use(middlewares.camelcaseKeys) | |
// ====================================== | |
// # Routes | |
// ====================================== | |
app.use('/', routes) | |
app.use(errorHandlers.notFound) | |
if (isProduction) { | |
app.use(errorHandlers.prodErrors) | |
} else { | |
app.use(errorHandlers.devErrors) | |
} | |
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
require('dotenv').config({ path: 'variables.env' }) | |
const mongoose = require('mongoose') | |
const isProduction = process.env.NODE_ENV === 'production' | |
const port = isProduction ? 8765 : 4000 | |
const database = isProduction ? process.env.MONGO_URL : process.env.MONGO_URL_LOCAL | |
mongoose.connect(database) | |
mongoose.Promise = global.Promise | |
mongoose.connection.on('error', (err) => { | |
console.error(`π π« π π« π π« π π« β ${err.message}`) | |
}) | |
require('./models/Course') | |
require('./models/User') | |
const app = require('./server') | |
app.listen(port, '127.0.0.1', () => { | |
console.log('app listening on http://' + 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
import test from 'ava' | |
import mongoose from 'mongoose' | |
import request from 'supertest' | |
import { MongoDBServer } from 'mongomem' | |
import { createFixtures, dropFixtures } from '../../fixtures/' | |
const setupMongoose = async _ => { | |
const db = new mongoose.Mongoose() | |
db.Promise = global.Promise | |
await db.connect(await MongoDBServer.getConnectionString()) | |
require('../../models/Course') | |
require('../../models/User') | |
for (const name of mongoose.modelNames()) { | |
db.model(name, mongoose.model(name).schema) | |
} | |
return db | |
} | |
test.before('start server', async t => await MongoDBServer.start()) | |
test.beforeEach(async t => { | |
const db = await setupMongoose() | |
const User = db.model('User') | |
const Course = db.model('Course') | |
await createFixtures(Course, User) | |
const app = require('../../server') | |
t.context.db = db | |
t.context.app = app | |
}) | |
test('Pass POST v1/users/testing', async t => { | |
const { app } = t.context | |
const res = await request(app) | |
.post('/api/v1/users/testing') | |
.send({ | |
email: 'testing@email.com' | |
}) | |
t.is(res.status, 200) | |
}) | |
test.after.always('cleanup', t => MongoDBServer.tearDown()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment