[
  {
    "path": ".gitignore",
    "content": "# OSX\n.DS_Store\n\n# Logs\nlogs\n*.log\n\n# Dependency directory\n# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git\nnode_modules/"
  },
  {
    "path": "LICENSE",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2017 Kelvin Nguyen\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE."
  },
  {
    "path": "README.md",
    "content": "# Express Env Example\n\nThis is an express environment example that I wrote to demonstrate modular Express routing patterns. This environment enables easy extensibility, scalability, and proper software design practices.\n\n\n**03/13/2018 Update**: An improved/advanced version of this architecture will be uploaded in the near future, be on the lookout.\n\n## Running\nTo start and run the project/server, simply run the following command in your root directory:\n\n```\nnpm start\n```\n\n\n## Sample endpoints\n\n### Views\n - `/` - Default route that redirects to the home page\n - `/home` - Render the home page sample\n - `/home/info` - Render the home page sample\n - `/error` - Render the error page sample\n\n### API\n - `/api/v1/dogs` - Get all dogs\n - `/api/v1/dogs/:id` - Get a dog with the specified id\n - `/api/v2/animals/dogs` - Get all dogs\n - `/api/v2/animals/dogs/:id` - Get a dog with the specified id\n"
  },
  {
    "path": "app/views/error/index.hbs",
    "content": "<h1>Oh shoot, looks like you ran into an error.</h1>\n<h2>This is probably your fault...</h2>"
  },
  {
    "path": "app/views/home/index.hbs",
    "content": "<h1>Welcome to this express environment sample by Kelvin!</h1>"
  },
  {
    "path": "app/views/home/info.hbs",
    "content": "<h1>You can follow more of Kelvin's projects on his blog: <a href=\"https://www.caffeinecoding.com\">Caffeine Coding!</a></h1>"
  },
  {
    "path": "app/views/layouts/default.hbs",
    "content": "<!DOCTYPE html>\n<html>\n<head>\n    <title>{{title}}</title>\n</head>\n<body>\n    {{{body}}}\n</body>\n</html>\n"
  },
  {
    "path": "configs/index.js",
    "content": "'use strict';\n\nconst\n    _ = require('lodash'),\n    env = process.env.NODE_ENV || 'local',\n    envConfig = require('./' + env);\n\nlet defaultConfig = {\n    env: env\n};\n\nmodule.exports = _.merge(defaultConfig, envConfig);\n"
  },
  {
    "path": "configs/local.js",
    "content": "'use strict';\n\nlet localConfig = {\n    hostname: 'localhost',\n    port: 3000,\n    viewDir: './app/views'\n};\n\nmodule.exports = localConfig;\n"
  },
  {
    "path": "index.js",
    "content": "'use strict';\n\nconst\n    server = require('./server')(),\n    config = require('./configs');\n\nserver.create(config);\nserver.start();"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"express-env-sample\",\n  \"version\": \"1.0.0\",\n  \"description\": \"App example of express environment\",\n  \"main\": \"index.js\",\n  \"scripts\": {\n    \"start\": \"node index.js\"\n  },\n  \"engines\": {\n    \"node\": \"^4.2.0\"\n  },\n  \"author\": \"Kelvin Nguyen\",\n  \"dependencies\": {\n    \"body-parser\": \"^1.16.0\",\n    \"express\": \"^4.14.1\",\n    \"express-handlebars\": \"^3.0.0\",\n    \"lodash\": \"^4.17.4\"\n  }\n}\n"
  },
  {
    "path": "server/controllers/apis/animals/index.js",
    "content": "'use strict';\n\nconst\n    express = require('express'),\n    dogController = require('../dogs');\n\nlet router = express.Router();\n\nrouter.use('/dogs', dogController);\n\nmodule.exports = router;"
  },
  {
    "path": "server/controllers/apis/dogs/index.js",
    "content": "'use strict';\n\nconst\n    express = require('express'),\n    dogService = require('../../../services/dogs');\n\nlet router = express.Router();\n\nrouter.get('/', dogService.getDogs);\nrouter.get('/:id', dogService.getDogWithId);\n\nmodule.exports = router;"
  },
  {
    "path": "server/controllers/error.js",
    "content": "'use strict';\n\nfunction index (req, res) {\n    res.render('error/index', {\n        title: 'Error'\n    });\n}\n\nmodule.exports = {\n    index: index\n};"
  },
  {
    "path": "server/controllers/home.js",
    "content": "'use strict';\n\nfunction index (req, res) {\n    res.render('home/index', {\n        title: 'Home'\n    });\n}\n\nfunction info (req, res) {\n    res.render('home/info', {\n        title: 'More info'\n    });\n}\n\nmodule.exports = {\n    index: index,\n    info: info\n};"
  },
  {
    "path": "server/index.js",
    "content": "'use strict';\n\nconst\n    express = require('express'),\n    expressHandlebars = require('express-handlebars'),\n    bodyParser = require('body-parser');\n\nmodule.exports = function() {\n    let server = express(),\n        create,\n        start;\n\n    create = function(config) {\n        let routes = require('./routes');\n\n        // Server settings\n        server.set('env', config.env);\n        server.set('port', config.port);\n        server.set('hostname', config.hostname);\n        server.set('viewDir', config.viewDir);\n\n        // Returns middleware that parses json\n        server.use(bodyParser.json());\n\n        // Setup view engine\n        server.engine('.hbs', expressHandlebars({\n            defaultLayout: 'default',\n            layoutsDir: config.viewDir + '/layouts',\n            extname: '.hbs'\n        }));\n        server.set('views', server.get('viewDir'));\n        server.set('view engine', '.hbs');\n\n        // Set up routes\n        routes.init(server);\n    };\n\n    start = function() {\n        let hostname = server.get('hostname'),\n            port = server.get('port');\n\n        server.listen(port, function () {\n            console.log('Express server listening on - http://' + hostname + ':' + port);\n        });\n    };\n\n    return {\n        create: create,\n        start: start\n    };\n};\n"
  },
  {
    "path": "server/routes/apis/index.js",
    "content": "'use strict';\n\nconst\n    express = require('express'),\n    v1ApiController = require('./v1'),\n    v2ApiController = require('./v2');\n\nlet router = express.Router();\n\nrouter.use('/v1', v1ApiController);\nrouter.use('/v2', v2ApiController);\n\nmodule.exports = router;"
  },
  {
    "path": "server/routes/apis/v1/index.js",
    "content": "'use strict';\n\nconst\n    express = require('express'),\n    dogsController = require('../../../controllers/apis/dogs');\n\nlet router = express.Router();\n\nrouter.use('/dogs', dogsController);\n\nmodule.exports = router;"
  },
  {
    "path": "server/routes/apis/v2/index.js",
    "content": "'use strict';\n\nconst\n    express = require('express'),\n    animalsController = require('../../../controllers/apis/animals');\n\nlet router = express.Router();\n\nrouter.use('/animals', animalsController);\n\nmodule.exports = router;"
  },
  {
    "path": "server/routes/error.js",
    "content": "'use strict';\n\nconst\n    express = require('express'),\n    errorController = require('../controllers/error');\n\nlet router = express.Router();\n\nrouter.get('/', errorController.index);\n\nmodule.exports = router;"
  },
  {
    "path": "server/routes/home.js",
    "content": "'use strict';\n\nconst\n    express = require('express'),\n    homeController = require('../controllers/home');\n\nlet router = express.Router();\n\nrouter.get('/', homeController.index);\nrouter.get('/info', homeController.info);\n\nmodule.exports = router;"
  },
  {
    "path": "server/routes/index.js",
    "content": "'use strict';\n\nconst\n    apiRoute = require('./apis'),\n    homeRoute = require('./home'),\n    errorRoute = require('./error');\n\nfunction init(server) {\n    server.get('*', function (req, res, next) {\n        console.log('Request was made to: ' + req.originalUrl);\n        return next();\n    });\n\n    server.get('/', function (req, res) {\n        res.redirect('/home');\n    });\n\n    server.use('/api', apiRoute);\n    server.use('/home', homeRoute);\n    server.use('/error', errorRoute);\n}\n\nmodule.exports = {\n    init: init\n};"
  },
  {
    "path": "server/services/dogs/index.js",
    "content": "'use strict';\n\nconst\n    dogs = [{\n        id: 1,\n        name: 'Corgi',\n        origin: 'Wales',\n        breeds: [\n            'Pembroke',\n            'Cardigan'\n        ]\n    }, {\n        id: 2,\n        name: 'Husky',\n        breeds: [\n            'Alaskan',\n            'Siberian',\n            'Labrador',\n            'Sakhalin'\n        ]\n    }];\n\n\nfunction getDogs(req, res) {\n    res.json(dogs);\n}\n\nfunction getDogWithId(req, res) {\n    let id = req.params.id || 0,\n        result = {};\n\n     for (let i = 0; i < dogs.length; i++) {\n        if (dogs[i].id == id) {\n            result = dogs[i];\n            break;\n        }\n     }\n\n     res.json(result);\n}\n\nmodule.exports = {\n    getDogs: getDogs,\n    getDogWithId: getDogWithId\n};\n"
  }
]