├── .env ├── .gitignore ├── README.md ├── app ├── models │ └── user.js └── routes │ ├── index.js │ └── users.js ├── config ├── environments │ └── development.js └── initializers │ ├── database.js │ └── server.js ├── index.js └── package.json /.env: -------------------------------------------------------------------------------- 1 | NODE_ENV=development 2 | 3 | NODE_PORT=4000 4 | NODE_HTTPS=false 5 | 6 | NODE_SSL_KEY=/path/to/key 7 | NODE_SSL_CERT=/path/to/cert 8 | 9 | SESSION_SECRET=fewgoodmen 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # express-restful-api 2 | Repo used as example for the blog post on www.codekitchen.ca 3 | 4 | Find details here: http://www.codekitchen.ca/guide-to-structuring-and-building-a-restful-api-using-express-4/ 5 | -------------------------------------------------------------------------------- /app/models/user.js: -------------------------------------------------------------------------------- 1 | // app/models/user.js 2 | // INITILIAZE your model here 3 | // var User = new Model() 4 | 5 | // module.exports = User; 6 | -------------------------------------------------------------------------------- /app/routes/index.js: -------------------------------------------------------------------------------- 1 | var changeCase = require('change-case'); 2 | var express = require('express'); 3 | var routes = require('require-dir')(); 4 | 5 | module.exports = function(app) { 6 | 'use strict'; 7 | 8 | // Initialize all routes 9 | Object.keys(routes).forEach(function(routeName) { 10 | var router = express.Router(); 11 | // You can add some middleware here 12 | // router.use(someMiddleware); 13 | 14 | // Initialize the route to add its functionality to router 15 | require('./' + routeName)(router); 16 | 17 | // Add router to the speficied route name in the app 18 | app.use('/' + changeCase.paramCase(routeName), router); 19 | }); 20 | }; 21 | 22 | -------------------------------------------------------------------------------- /app/routes/users.js: -------------------------------------------------------------------------------- 1 | // app/routes/users.js 2 | 3 | module.exports = function(router) { 4 | 'use strict'; 5 | // This will handle the url calls for /users/:user_id 6 | router.route('/:userId') 7 | .get(function(req, res, next) { 8 | // Return user 9 | }) 10 | .put(function(req, res, next) { 11 | // Update user 12 | }) 13 | .patch(function(req, res,next) { 14 | // Patch 15 | }) 16 | .delete(function(req, res, next) { 17 | // Delete record 18 | }); 19 | 20 | router.route('/') 21 | .get(function(req, res, next) { 22 | // Logic for GET /users routes 23 | }).post(function(req, res, next) { 24 | // Create new user 25 | }); 26 | }; 27 | -------------------------------------------------------------------------------- /config/environments/development.js: -------------------------------------------------------------------------------- 1 | var nconf = require('nconf'); 2 | nconf.set('url', 'mywebsite.com'); 3 | 4 | nconf.set('database', { 5 | user: 'username', 6 | password: 'password', 7 | server: 'url' 8 | }); 9 | -------------------------------------------------------------------------------- /config/initializers/database.js: -------------------------------------------------------------------------------- 1 | // config/initializers/database.js 2 | 3 | module.exports = function(cb) { 4 | 'use strict'; 5 | // Initialize the component here then call the callback 6 | // More logic 7 | // 8 | // Return the call back 9 | cb(); 10 | }; 11 | -------------------------------------------------------------------------------- /config/initializers/server.js: -------------------------------------------------------------------------------- 1 | // config/initializers/server.js 2 | 3 | var express = require('express'); 4 | var path = require('path'); 5 | // Local dependecies 6 | var config = require('nconf'); 7 | 8 | // create the express app 9 | // configure middlewares 10 | var bodyParser = require('body-parser'); 11 | var morgan = require('morgan'); 12 | var logger = require('winston'); 13 | var app; 14 | 15 | var start = function(cb) { 16 | 'use strict'; 17 | // Configure express 18 | app = express(); 19 | 20 | app.use(morgan('common')); 21 | app.use(bodyParser.urlencoded({extended: true})); 22 | app.use(bodyParser.json({type: '*/*'})); 23 | 24 | logger.info('[SERVER] Initializing routes'); 25 | require('../../app/routes/index')(app); 26 | 27 | app.use(express.static(path.join(__dirname, 'public'))); 28 | 29 | // Error handler 30 | app.use(function(err, req, res, next) { 31 | res.status(err.status || 500); 32 | res.json({ 33 | message: err.message, 34 | error: (app.get('env') === 'development' ? err : {}) 35 | }); 36 | next(err); 37 | }); 38 | 39 | app.listen(config.get('NODE_PORT')); 40 | logger.info('[SERVER] Listening on port ' + config.get('NODE_PORT')); 41 | 42 | if (cb) { 43 | return cb(); 44 | } 45 | }; 46 | 47 | module.exports = start; 48 | 49 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | // /index.js 2 | 'use strict'; 3 | 4 | var server = require('./config/initializers/server'); 5 | var nconf = require('nconf'); 6 | var async = require('async'); 7 | var logger = require('winston'); 8 | 9 | // Load Environment variables from .env file 10 | require('dotenv').load(); 11 | 12 | // Set up configs 13 | nconf.use('memory'); 14 | // First load command line arguments 15 | nconf.argv(); 16 | // Load environment variables 17 | nconf.env(); 18 | // Load config file for the environment 19 | require('./config/environments/' + nconf.get('NODE_ENV')); 20 | 21 | logger.info('[APP] Starting server initialization'); 22 | 23 | // Initialize Modules 24 | async.series([ 25 | function initializeDBConnection(callback) { 26 | require('./config/initializers/database')(callback); 27 | }, 28 | function startServer(callback) { 29 | server(callback); 30 | }], function(err) { 31 | if (err) { 32 | logger.error('[APP] initialization failed', err); 33 | } else { 34 | logger.info('[APP] initialized SUCCESSFULLY'); 35 | } 36 | } 37 | ); 38 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "express-restful-api", 3 | "version": "1.0.0", 4 | "description": "Boilerplate for creating a RESTful API in node.js", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/tolgaek/express-restful-api.git" 12 | }, 13 | "author": "Tolga Ekmen (https://github.com/tolgaek)", 14 | "license": "MIT", 15 | "bugs": { 16 | "url": "https://github.com/tolgaek/express-restful-api/issues" 17 | }, 18 | "homepage": "https://github.com/tolgaek/express-restful-api", 19 | "dependencies": { 20 | "async": "^1.2.1", 21 | "body-parser": "^1.13.1", 22 | "change-case": "^2.3.0", 23 | "dotenv": "^1.2.0", 24 | "express": "^4.13.0", 25 | "morgan": "^1.6.0", 26 | "nconf": "^0.7.1", 27 | "require-dir": "^0.3.0", 28 | "underscore": "^1.8.3", 29 | "winston": "^1.0.0" 30 | } 31 | } 32 | --------------------------------------------------------------------------------