-
-
Save laktek/40ffca7935076775dd06659cc2dbb771 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 mongoose = require('mongoose') | |
class Server { | |
constructor(dbConn) => { | |
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) | |
} | |
mongoose.connect(dbConn) | |
mongoose.Promise = global.Promise | |
mongoose.connection.on('error', (err) => { | |
console.error(`π π« π π« π π« π π« β ${err.message}`) | |
}) | |
this.app = app | |
} | |
start() => { | |
const port = isProduction ? 8765 : 4000 | |
this.app.listen(port, '127.0.0.1', () => { | |
console.log('app listening on http://' + port) | |
}) | |
} | |
} | |
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 isProduction = process.env.NODE_ENV === 'production' | |
const database = isProduction ? process.env.MONGO_URL : process.env.MONGO_URL_LOCAL | |
const Server = require('./server') | |
const app = new Server(database); | |
// I don't know what's happening inside the models, | |
// I assume they do some bootstrapping through side-effects | |
require('./models/Course') | |
require('./models/User') | |
app.start() |
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() | |
const Server = require('../../server') | |
t.context.app = new Server(await MongoDBServer.getConnectionString()).app | |
}) | |
test.beforeEach(async t => { | |
const db = await setupMongoose() | |
const User = db.model('User') | |
const Course = db.model('Course') | |
await createFixtures(Course, User) | |
}) | |
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