diff --git a/test/api/v3/unit/middlewares/notFound.test.js b/test/api/v3/unit/middlewares/notFound.test.js index 5f9922d38c..f2f6082acf 100644 --- a/test/api/v3/unit/middlewares/notFound.test.js +++ b/test/api/v3/unit/middlewares/notFound.test.js @@ -1,30 +1,13 @@ -import { - generateRes, - generateReq, - generateNext, -} from '../../../../helpers/api-unit.helper'; +import { requester } from '../../../../helpers/api-integration.helper'; -import notFoundHandler from '../../../../../website/src/middlewares/api-v3/notFound'; +describe('notFound Middleware', () => { + it('returns a 404 error when the resource is not found', () => { + let request = requester().get('/api/v3/dummy-url'); -import { NotFound } from '../../../../../website/src/libs/api-v3/errors'; - -describe('notFoundHandler', () => { - let res, req, next; - - beforeEach(() => { - res = generateRes(); - req = generateReq(); - next = generateNext(); - }); - - xit('sends NotFound error if the resource isn\'t found', () => { - expect(res.status).to.be.calledOnce; - expect(res.json).to.be.calledOnce; - - expect(res.status).to.be.calledWith(404); - expect(res.json).to.be.calledWith({ - error: 'NotFound', - message: 'Not found.', - }); - }); + return expect(request) + .to.eventually.be.rejected.and.eql({ + error: "NotFound", + message: "Not found.", + }); + }); }); diff --git a/website/src/controllers/api-v3/example.js b/website/src/controllers/api-v3/example.js index 578ce21655..6ad474a5f2 100644 --- a/website/src/controllers/api-v3/example.js +++ b/website/src/controllers/api-v3/example.js @@ -22,11 +22,11 @@ let api = {}; */ api.exampleRoute = { method: 'GET', - url: '/example/:param', + url: '/example/:id', middlewares: [], handler (req, res) { res.status(200).send({ - status: 'ok', + status: req.params.id, }); }, }; diff --git a/website/src/index.js b/website/src/index.js index 2440dde36f..a562dfa9c2 100644 --- a/website/src/index.js +++ b/website/src/index.js @@ -14,8 +14,6 @@ var IS_PROD = nconf.get('IS_PROD'); var IS_DEV = nconf.get('IS_DEV'); var cores = Number(nconf.get('WEB_CONCURRENCY')) || 0; -if (IS_DEV) Error.stackTraceLimit = Infinity; - // Setup the cluster module if (cores !== 0 && cluster.isMaster && (IS_DEV || IS_PROD)) { // Fork workers. If config.json has CORES=x, use that - otherwise, use all cpus-1 (production) diff --git a/website/src/libs/api-v3/logger.js b/website/src/libs/api-v3/logger.js index 3ab20ad685..0d00ca7f4b 100644 --- a/website/src/libs/api-v3/logger.js +++ b/website/src/libs/api-v3/logger.js @@ -14,6 +14,7 @@ if (IS_PROD) { logger .add(winston.transports.Console, { colorize: true, + prettyPrint: true, }); } diff --git a/website/src/libs/api-v3/setupRoutes.js b/website/src/libs/api-v3/setupRoutes.js index 4bce7b1e26..8c346577bc 100644 --- a/website/src/libs/api-v3/setupRoutes.js +++ b/website/src/libs/api-v3/setupRoutes.js @@ -2,6 +2,7 @@ import fs from 'fs'; import path from 'path'; import express from 'express'; import _ from 'lodash'; + const CONTROLLERS_PATH = path.join(__dirname, '/../../controllers/api-v3/'); let router = express.Router(); // eslint-disable-line new-cap @@ -20,4 +21,4 @@ fs }); }); -export default router; \ No newline at end of file +export default router; diff --git a/website/src/middlewares/api-v3/index.js b/website/src/middlewares/api-v3/index.js index 0041e2dd54..dfd19e5493 100644 --- a/website/src/middlewares/api-v3/index.js +++ b/website/src/middlewares/api-v3/index.js @@ -5,9 +5,15 @@ import errorHandler from './errorHandler'; import bodyParser from 'body-parser'; import routes from '../../libs/api-v3/setupRoutes'; import notFoundHandler from './notFound'; +import nconf from 'nconf'; +import morgan from 'morgan'; + +const IS_PROD = nconf.get('IS_PROD'); +const DISABLE_LOGGING = nconf.get('DISABLE_REQUEST_LOGGING'); export default function attachMiddlewares (app) { - // Parse query parameters and json bodies + if (!IS_PROD && !DISABLE_LOGGING) app.use(morgan('dev')); + // TODO handle errors app.use(bodyParser.urlencoded({ extended: true, // Uses 'qs' library as old connect middleware @@ -15,7 +21,7 @@ export default function attachMiddlewares (app) { app.use(bodyParser.json()); app.use(analytics); - app.use(routes); + app.use('/api/v3', routes); app.use(notFoundHandler); // Error handler middleware, define as the last one diff --git a/website/src/server.js b/website/src/server.js index 21744c0813..aa0013d89e 100644 --- a/website/src/server.js +++ b/website/src/server.js @@ -89,7 +89,7 @@ app.use(domainMiddleware(server, mongoose)); // Matches all request except the ones going to /api/v3/** app.all(/^(?!\/api\/v3).+/i, oldApp); // Matches all requests going to /api/v3 -app.all('/api/v3', newApp); +app.all('/api/*', newApp); // Mount middlewares for the new app attachMiddlewares(newApp);