da1f9437ab
- Extract Express app creation into src/createApp.js to enable testing without starting the server
- Add Jest, Supertest, test helpers (createTestApp, auth token generator)
- Write 14 integration tests for skydive/jumps: auth protection, CRUD, ownership checks
Bugs caught and fixed by the test suite:
- MongoDB models: author field typed as Schema.Types.UUID (Mongoose 6 rejects string UUIDs) → changed to String
- Jump.toJSONFor: called toProfileJSONFor on a UUID string → guarded with typeof check
- exceptions.handler: used err.statusCode but express-jwt throws err.status → returns 500 on 401 errors
- Jump.count(query): deprecated Mongoose method was ignoring the filter → replaced with countDocuments
- jumps.routes GET /: $or query included non-schema field "title" stripped by strictQuery, leaving {} (match-all) → replaced with slug/lieu search
- $regex: was passing a JS RegExp object which serializes to {} in MongoDB → now passes raw string
60 lines
2.1 KiB
JavaScript
60 lines
2.1 KiB
JavaScript
var appEnv = process.env.APP_ENV;
|
|
if (appEnv == undefined) {
|
|
appEnv = 'development';
|
|
}
|
|
require('dotenv').config({ path: `config/env/.env.${appEnv}` });
|
|
|
|
const { promisify } = require('util'),
|
|
chalk = require('chalk'),
|
|
morgan = require('morgan');
|
|
|
|
const DateUtilities = require('./src/utils/dateUtilities'),
|
|
exceptionHandler = require('./src/middlewares/exceptions.handler'),
|
|
connectDb = require('./src/database/connectDb'),
|
|
config = require('./src/config'),
|
|
createApp = require('./src/createApp');
|
|
|
|
const app = createApp();
|
|
|
|
// Morgan status code color config
|
|
morgan.token('statusColor', (req, res) => {
|
|
const status = (typeof res.headersSent !== 'boolean' ? Boolean(res.header) : res.headersSent)
|
|
? res.statusCode
|
|
: undefined;
|
|
return status >= 500 ? chalk.red(status)
|
|
: status >= 429 ? chalk.magenta(status)
|
|
: status >= 400 ? chalk.yellow(status)
|
|
: status >= 300 ? chalk.cyan(status)
|
|
: status >= 200 ? chalk.green(status)
|
|
: chalk.underline(status);
|
|
});
|
|
app.use(morgan('[:date[iso]] ":method :url HTTP/:http-version" :statusColor :res[content-length] ":referrer" ":user-agent" :remote-addr'));
|
|
|
|
require('./src/database/models/mongo');
|
|
config.initAuth();
|
|
|
|
const setupRoutes = (app) => {
|
|
app.use(require('./src/routes'));
|
|
app.use(exceptionHandler.notFoundErrorHandler);
|
|
app.use(exceptionHandler.logErrorHandler);
|
|
app.use(exceptionHandler.fianlErrorHandler);
|
|
};
|
|
|
|
const startServer = async () => {
|
|
try {
|
|
await connectDb.connectAllDb();
|
|
console.log(`${DateUtilities.getDateTimeISOString()}`, chalk.green('All database connections established, relationships loaded'));
|
|
|
|
setupRoutes(app);
|
|
|
|
const port = process.env.SERVER_PORT || 3200;
|
|
await promisify(app.listen).bind(app)(port);
|
|
console.log(`${DateUtilities.getDateTimeISOString()}`, chalk.green(`${appEnv} API server listening on port ${port}`));
|
|
} catch (error) {
|
|
console.error(`${DateUtilities.getDateTimeISOString()}`, chalk.red('Failed to start server:'), error);
|
|
process.exit(1);
|
|
}
|
|
};
|
|
|
|
startServer();
|