Skip to content

Commit b2cf214

Browse files
committed
Support env variable through .env file
1 parent 934d111 commit b2cf214

File tree

4 files changed

+55
-33
lines changed

4 files changed

+55
-33
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ pids
1414
# Babel ES6 compiles files
1515
dist
1616

17+
# env variable file
18+
.env
19+
1720
# Directory for instrumented libs generated by jscoverage/JSCover
1821
lib-cov
1922

config/env.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import path from 'path';
2+
3+
// load env variables defined in .env
4+
const config = require('dotenv').config() || {};
5+
6+
// root path of server
7+
config.rootPath = path.join(__dirname, '../');
8+
9+
console.log('loaded env');
10+
export default config;

index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
const app = require('./config/express'),
2-
debug = require('debug')('express-es6-rest-api-starter:index');
1+
import config from './config/env';
2+
import app from './config/express';
33

4-
app.listen(3000, () => {
5-
debug(`server started on port ${process.env.HTTP_PORT} in ${process.env.NODE_ENV} env`);
4+
const debug = require('debug')('express-es6-rest-api-starter:index');
5+
6+
// listen on port HTTP_PORT
7+
app.listen(config.HTTP_PORT, () => {
8+
debug(`started server on port ${config.HTTP_PORT} (${config.NODE_ENV})`);
69
});

package.json

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
11
{
2-
"name": "express-es6-rest-api-starter",
3-
"version": "0.0.0",
4-
"private": true,
5-
"scripts": {
6-
"start": "node ./bin/www"
7-
},
8-
"dependencies": {
9-
"body-parser": "~1.13.2",
10-
"compression": "^1.6.1",
11-
"cookie-parser": "~1.3.5",
12-
"cors": "^2.7.1",
13-
"debug": "^2.2.0",
14-
"express": "~4.13.1",
15-
"http-status": "^0.2.0",
16-
"jade": "~1.11.0",
17-
"method-override": "^2.3.5",
18-
"morgan": "~1.6.1",
19-
"serve-favicon": "~2.3.0"
20-
},
21-
"devDependencies": {
22-
"babel": "^5.8.23",
23-
"del": "^2.2.0",
24-
"gulp-babel": "^5.2.1",
25-
"gulp-load-plugins": "^1.2.0",
26-
"gulp-newer": "^1.1.0",
27-
"gulp-nodemon": "^2.0.6",
28-
"gulp-plumber": "^1.0.1",
29-
"gulp-sourcemaps": "^1.6.0"
30-
}
2+
"name": "express-es6-rest-api-starter",
3+
"version": "0.1.0",
4+
"description": "Starter project for an ES6 RESTful Express API",
5+
"main": "index.js",
6+
"private": false,
7+
"scripts": {
8+
"start": "gulp babel && node dist/index.js",
9+
"test": "echo \"Error: no test specified\" && exit 1"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "[email protected]:KunalKapadia/express-es6-rest-api-starter.git"
14+
},
15+
"author": "Kunal Kapadia <[email protected]>",
16+
"dependencies": {
17+
"body-parser": "~1.13.2",
18+
"compression": "^1.6.1",
19+
"cors": "^2.7.1",
20+
"debug": "^2.2.0",
21+
"dotenv": "^2.0.0",
22+
"express": "~4.13.1",
23+
"http-status": "^0.2.0",
24+
"method-override": "^2.3.5",
25+
"morgan": "~1.6.1"
26+
},
27+
"devDependencies": {
28+
"babel": "^5.8.23",
29+
"del": "^2.2.0",
30+
"gulp-babel": "^5.2.1",
31+
"gulp-load-plugins": "^1.2.0",
32+
"gulp-newer": "^1.1.0",
33+
"gulp-nodemon": "^2.0.6",
34+
"gulp-plumber": "^1.0.1",
35+
"gulp-sourcemaps": "^1.6.0"
36+
}
3137
}

0 commit comments

Comments
 (0)