├── Procfile ├── index.html ├── .env-sample ├── routes └── api-endpoint.js ├── util ├── Users.js └── mongo.js ├── .eslintrc.json ├── package.json ├── .gitignore ├── server.js └── README.md /Procfile: -------------------------------------------------------------------------------- 1 | web: npm run start 2 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | Tea, Earl Grey, hot. 2 | -------------------------------------------------------------------------------- /.env-sample: -------------------------------------------------------------------------------- 1 | MLAB_CONNECTION=your-db-connection-info 2 | -------------------------------------------------------------------------------- /routes/api-endpoint.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const router = express.Router(); 3 | 4 | router.post('/api-endpoint', (req, res) => { 5 | //do some api stuff here 6 | }); 7 | module.exports = router; 8 | -------------------------------------------------------------------------------- /util/Users.js: -------------------------------------------------------------------------------- 1 | var mongoose = require('mongoose'); 2 | var Schema = mongoose.Schema; 3 | 4 | var userSchema = new Schema({ 5 | id: { type: String, required: true, unique: true }, 6 | name: String 7 | }); 8 | 9 | var User = mongoose.model('User', userSchema); 10 | 11 | module.exports = User; 12 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "node": true, 4 | "es6": true 5 | }, 6 | "extends": ["eslint:recommended"], 7 | "parserOptions": { 8 | "ecmaFeatures": { 9 | "experimentalObjectRestSpread": true 10 | }, 11 | "sourceType": "module" 12 | }, 13 | "rules": { 14 | "indent": ["error", "tab"], 15 | "linebreak-style": ["error", "unix"], 16 | "quotes": ["error", "single"], 17 | "no-console": [0] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /util/mongo.js: -------------------------------------------------------------------------------- 1 | var mongoose = require('mongoose'); 2 | 3 | var db; 4 | module.exports = { 5 | connectToServer: function(callback) { 6 | var mongoDB = process.env.MLAB_CONNECTION; 7 | mongoose.Promise = global.Promise; 8 | mongoose.connect(mongoDB); 9 | db = mongoose.connection; 10 | db.on('error', console.error.bind(console, 'MongoDB connection error:')); 11 | return callback(); 12 | }, 13 | getDb: function() { 14 | return db; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "server", 3 | "version": "1.0.0", 4 | "description": "", 5 | "engines": { 6 | "node": "6.9.0" 7 | }, 8 | "main": "server.js", 9 | "scripts": { 10 | "test": "echo \"Error: no test specified\" && exit 1", 11 | "server": "nodemon server.js --exec babel-node", 12 | "heroku-postbuild": "npm install", 13 | "start": "babel-node server.js" 14 | }, 15 | "author": "", 16 | "license": "ISC", 17 | "dependencies": { 18 | "body-parser": "1.17.2", 19 | "cors": "2.8.3", 20 | "dotenv": "4.0.0", 21 | "express": "4.15.3", 22 | "http": "0.0.0", 23 | "https": "1.0.0", 24 | "lodash": "4.17.4", 25 | "mongodb": "2.2.27", 26 | "mongoose": "4.10.8", 27 | "morgan": "1.8.2", 28 | "path": "0.12.7" 29 | }, 30 | "devDependencies": { 31 | "babel": "6.23.0", 32 | "babel-cli": "6.26.0", 33 | "babel-preset-env": "1.6.0", 34 | "nodemon": "1.11.0" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | #Jay 2 | .env 3 | 4 | # OSX 5 | # 6 | .DS_Store 7 | 8 | # Xcode 9 | # 10 | build/ 11 | *.pbxuser 12 | !default.pbxuser 13 | *.mode1v3 14 | !default.mode1v3 15 | *.mode2v3 16 | !default.mode2v3 17 | *.perspectivev3 18 | !default.perspectivev3 19 | xcuserdata 20 | *.xccheckout 21 | *.moved-aside 22 | DerivedData 23 | *.hmap 24 | *.ipa 25 | *.xcuserstate 26 | project.xcworkspace 27 | 28 | # Android/IntelliJ 29 | # 30 | build/ 31 | .idea 32 | .gradle 33 | local.properties 34 | *.iml 35 | 36 | # node.js 37 | # 38 | node_modules/ 39 | npm-debug.log 40 | yarn-error.log 41 | 42 | # BUCK 43 | buck-out/ 44 | \.buckd/ 45 | *.keystore 46 | 47 | # fastlane 48 | # 49 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 50 | # screenshots whenever they are needed. 51 | # For more information about the recommended setup visit: 52 | # https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md 53 | 54 | fastlane/report.xml 55 | fastlane/Preview.html 56 | fastlane/screenshots 57 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | require('dotenv').config(); 2 | const express = require('express'); 3 | const path = require('path'); 4 | const http = require('http'); 5 | const cors = require('cors'); 6 | const morgan = require('morgan'); 7 | const bodyParser = require('body-parser'); 8 | const mongoUtil = require('./util/mongo'); 9 | 10 | const app = express(); 11 | const apiEndpoint = require('./routes/api-endpoint'); 12 | 13 | app.use(bodyParser.urlencoded({ extended: true })); 14 | app.use(bodyParser.json()); 15 | app.use(cors()); 16 | app.use(morgan('dev')); 17 | app.use(express.static(path.join(__dirname, 'dist'))); 18 | 19 | mongoUtil.connectToServer(function(err) { 20 | if (err) return console.log(err); 21 | }); 22 | 23 | app.use('/api', apiEndpoint); 24 | 25 | app.get('*', (req, res) => { 26 | res.sendFile(path.join(__dirname, 'index.html')); 27 | }); 28 | 29 | const port = process.env.PORT || '1337'; 30 | app.set('port', port); 31 | 32 | const server = http.createServer(app); 33 | 34 | server.listen(port, () => console.log(`API running on localhost:${port}`)); 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## :warning: This repo is no longer maintained 2 | 3 | # node-es6-heroku-boilerplate 4 | 5 | Simple boilerplate for running ES6+ features on a Node server - specifically on a Heroku environment, although there are only a couple of bits which have been altered to make the Heroku environment work which are: 6 | 7 | ## Heroku specific 8 | 9 | - The Procfile - used to execute a web hook on deployment 10 | - `"start": "babel-node server.js"` in the package.json file 11 | 12 | ## Front-end app folder 13 | 14 | There's a directory, `/app` which can be used to hold a Vue/React/React-Native/whatever front end system. These can be decoupled completely from the back end, but sometimes it can be good to keep them together in source control while it's being developed (I like to anyway). To read more, [see here](https://github.com/mars/heroku-cra-node). 15 | 16 | ## Node server 17 | 18 | The node server has the following basic elements which I end up using in every project: 19 | 20 | - Uses routes in a separate folder to keep things clean! These are great for API endpoints, or can be used to group pages if serving HTML content to a website. 21 | - Uses Mongoose which is created and connected in a separate folder, `/util`, alongside a **User** model which is exported for use around the server. 22 | - Uses ESLint with pre-defined Node and ES6 rules. 23 | - Uses **Environment variables** with the help of the **dotenv** npm package - these are hidden fron Git source control so secret information isn't pushed publically. When used with cloud systems (like Heroku) the environment variables are added separately. With Heroku this can be done in the admin area or CLI. 24 | - I've added a `.env-sample` file here to show in action (enables the use of `process.env.MLAB_CONNECTION` in the `/util/mongo.js` file). It should be called `.env`. 25 | --------------------------------------------------------------------------------