Repository: kelyvin/express-env-example Branch: master Commit: d519a3542f41 Files: 23 Total size: 7.8 KB Directory structure: gitextract_7dxvdjfg/ ├── .gitignore ├── LICENSE ├── README.md ├── app/ │ └── views/ │ ├── error/ │ │ └── index.hbs │ ├── home/ │ │ ├── index.hbs │ │ └── info.hbs │ └── layouts/ │ └── default.hbs ├── configs/ │ ├── index.js │ └── local.js ├── index.js ├── package.json └── server/ ├── controllers/ │ ├── apis/ │ │ ├── animals/ │ │ │ └── index.js │ │ └── dogs/ │ │ └── index.js │ ├── error.js │ └── home.js ├── index.js ├── routes/ │ ├── apis/ │ │ ├── index.js │ │ ├── v1/ │ │ │ └── index.js │ │ └── v2/ │ │ └── index.js │ ├── error.js │ ├── home.js │ └── index.js └── services/ └── dogs/ └── index.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # OSX .DS_Store # Logs logs *.log # Dependency directory # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git node_modules/ ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Copyright (c) 2017 Kelvin Nguyen Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # Express Env Example This 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. **03/13/2018 Update**: An improved/advanced version of this architecture will be uploaded in the near future, be on the lookout. ## Running To start and run the project/server, simply run the following command in your root directory: ``` npm start ``` ## Sample endpoints ### Views - `/` - Default route that redirects to the home page - `/home` - Render the home page sample - `/home/info` - Render the home page sample - `/error` - Render the error page sample ### API - `/api/v1/dogs` - Get all dogs - `/api/v1/dogs/:id` - Get a dog with the specified id - `/api/v2/animals/dogs` - Get all dogs - `/api/v2/animals/dogs/:id` - Get a dog with the specified id ================================================ FILE: app/views/error/index.hbs ================================================

Oh shoot, looks like you ran into an error.

This is probably your fault...

================================================ FILE: app/views/home/index.hbs ================================================

Welcome to this express environment sample by Kelvin!

================================================ FILE: app/views/home/info.hbs ================================================

You can follow more of Kelvin's projects on his blog: Caffeine Coding!

================================================ FILE: app/views/layouts/default.hbs ================================================ {{title}} {{{body}}} ================================================ FILE: configs/index.js ================================================ 'use strict'; const _ = require('lodash'), env = process.env.NODE_ENV || 'local', envConfig = require('./' + env); let defaultConfig = { env: env }; module.exports = _.merge(defaultConfig, envConfig); ================================================ FILE: configs/local.js ================================================ 'use strict'; let localConfig = { hostname: 'localhost', port: 3000, viewDir: './app/views' }; module.exports = localConfig; ================================================ FILE: index.js ================================================ 'use strict'; const server = require('./server')(), config = require('./configs'); server.create(config); server.start(); ================================================ FILE: package.json ================================================ { "name": "express-env-sample", "version": "1.0.0", "description": "App example of express environment", "main": "index.js", "scripts": { "start": "node index.js" }, "engines": { "node": "^4.2.0" }, "author": "Kelvin Nguyen", "dependencies": { "body-parser": "^1.16.0", "express": "^4.14.1", "express-handlebars": "^3.0.0", "lodash": "^4.17.4" } } ================================================ FILE: server/controllers/apis/animals/index.js ================================================ 'use strict'; const express = require('express'), dogController = require('../dogs'); let router = express.Router(); router.use('/dogs', dogController); module.exports = router; ================================================ FILE: server/controllers/apis/dogs/index.js ================================================ 'use strict'; const express = require('express'), dogService = require('../../../services/dogs'); let router = express.Router(); router.get('/', dogService.getDogs); router.get('/:id', dogService.getDogWithId); module.exports = router; ================================================ FILE: server/controllers/error.js ================================================ 'use strict'; function index (req, res) { res.render('error/index', { title: 'Error' }); } module.exports = { index: index }; ================================================ FILE: server/controllers/home.js ================================================ 'use strict'; function index (req, res) { res.render('home/index', { title: 'Home' }); } function info (req, res) { res.render('home/info', { title: 'More info' }); } module.exports = { index: index, info: info }; ================================================ FILE: server/index.js ================================================ 'use strict'; const express = require('express'), expressHandlebars = require('express-handlebars'), bodyParser = require('body-parser'); module.exports = function() { let server = express(), create, start; create = function(config) { let routes = require('./routes'); // Server settings server.set('env', config.env); server.set('port', config.port); server.set('hostname', config.hostname); server.set('viewDir', config.viewDir); // Returns middleware that parses json server.use(bodyParser.json()); // Setup view engine server.engine('.hbs', expressHandlebars({ defaultLayout: 'default', layoutsDir: config.viewDir + '/layouts', extname: '.hbs' })); server.set('views', server.get('viewDir')); server.set('view engine', '.hbs'); // Set up routes routes.init(server); }; start = function() { let hostname = server.get('hostname'), port = server.get('port'); server.listen(port, function () { console.log('Express server listening on - http://' + hostname + ':' + port); }); }; return { create: create, start: start }; }; ================================================ FILE: server/routes/apis/index.js ================================================ 'use strict'; const express = require('express'), v1ApiController = require('./v1'), v2ApiController = require('./v2'); let router = express.Router(); router.use('/v1', v1ApiController); router.use('/v2', v2ApiController); module.exports = router; ================================================ FILE: server/routes/apis/v1/index.js ================================================ 'use strict'; const express = require('express'), dogsController = require('../../../controllers/apis/dogs'); let router = express.Router(); router.use('/dogs', dogsController); module.exports = router; ================================================ FILE: server/routes/apis/v2/index.js ================================================ 'use strict'; const express = require('express'), animalsController = require('../../../controllers/apis/animals'); let router = express.Router(); router.use('/animals', animalsController); module.exports = router; ================================================ FILE: server/routes/error.js ================================================ 'use strict'; const express = require('express'), errorController = require('../controllers/error'); let router = express.Router(); router.get('/', errorController.index); module.exports = router; ================================================ FILE: server/routes/home.js ================================================ 'use strict'; const express = require('express'), homeController = require('../controllers/home'); let router = express.Router(); router.get('/', homeController.index); router.get('/info', homeController.info); module.exports = router; ================================================ FILE: server/routes/index.js ================================================ 'use strict'; const apiRoute = require('./apis'), homeRoute = require('./home'), errorRoute = require('./error'); function init(server) { server.get('*', function (req, res, next) { console.log('Request was made to: ' + req.originalUrl); return next(); }); server.get('/', function (req, res) { res.redirect('/home'); }); server.use('/api', apiRoute); server.use('/home', homeRoute); server.use('/error', errorRoute); } module.exports = { init: init }; ================================================ FILE: server/services/dogs/index.js ================================================ 'use strict'; const dogs = [{ id: 1, name: 'Corgi', origin: 'Wales', breeds: [ 'Pembroke', 'Cardigan' ] }, { id: 2, name: 'Husky', breeds: [ 'Alaskan', 'Siberian', 'Labrador', 'Sakhalin' ] }]; function getDogs(req, res) { res.json(dogs); } function getDogWithId(req, res) { let id = req.params.id || 0, result = {}; for (let i = 0; i < dogs.length; i++) { if (dogs[i].id == id) { result = dogs[i]; break; } } res.json(result); } module.exports = { getDogs: getDogs, getDogWithId: getDogWithId };