Skip to content

Commit

Permalink
Implement some best pratices
Browse files Browse the repository at this point in the history
  • Loading branch information
alexvndre committed Nov 13, 2017
1 parent 4c48b31 commit 0e9e875
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 34 deletions.
10 changes: 6 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include ./.env

.PHONY: build check clean help lint install start test test-coverage

.DEFAULT_GOAL := help
Expand All @@ -18,7 +20,7 @@ clean: ## clean artifacts

help: ## provide help to you
@echo "Please use \`make <target>' where <target> is one of"
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-30s\033[0m %s\n", $$1, $$2}'
@echo "$$(grep -hE '^\S+:.*##' $(MAKEFILE_LIST) | sed -e 's/:.*##\s*/:/' -e 's/^\(.\+\):\(.*\)/\1:\2/' | column -c2 -t -s :)"

lint: ## check the quality code and ES6 integration
@echo " > Linting the source"
Expand All @@ -33,14 +35,14 @@ install: ## install dependencies
start: ## start the web server
@echo " > Starting the project"
@$(MAKE) -s build
@export PORT=$(PORT) && export NODE_ENV=$(ENVIRONMENT) && node $(BUILD_DIRECTORY)/index.js
@node $(BUILD_DIRECTORY)/index.js

test: ## launch tests
@echo " > Testing the project"
@$(MAKE) -s build
@export PORT=$(PORT) && export NODE_ENV=$(ENVIRONMENT_TEST) && $(MOCHA) --require babel-core/register --recursive --exit
@export PORT=0 && export NODE_ENV=test && $(MOCHA) --require babel-core/register --recursive --exit

test-coverage: ## launch tests with coverage
@echo " > Testing with coverage"
@$(MAKE) -s build
@export PORT=$(PORT) && export NODE_ENV=$(ENVIRONMENT_TEST) && $(BABEL_NODE) $(BABEL_ISTANBUL) cover $(MOCHA_) --report html --report text --check-coverage -- --recursive
@export PORT=0 && export NODE_ENV=test && $(BABEL_NODE) $(BABEL_ISTANBUL) cover $(MOCHA_) --report html --report text --check-coverage -- --recursive
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Run tests to verify the installation.

### Run the project

The project is available on [http://127.0.0.1:3000](http://127.0.0.1:3000).
The project is available on [http://127.0.0.1:<PORT>](http://127.0.0.1:<PORT>).

`$ make start`

Expand Down
5 changes: 5 additions & 0 deletions config/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"app": {
"name": "expressjs-starter-pack"
}
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
"dependencies": {
"body-parser": "^1.17.1",
"cluster": "^0.7.7",
"config": "^1.27.0",
"cors": "^2.8.4",
"express": "^4.15.2",
"express-validator": "^4.3.0",
"normalize-port": "^1.0.0",
"winston": "^2.3.1"
},
"devDependencies": {
Expand Down
7 changes: 1 addition & 6 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import bodyParser from 'body-parser';
import cors from 'cors';
import express from 'express';
import expressValidator from 'express-validator';
import config from './config/index';
import controllers from './controller/index';
import logger from './helper/logger';

Expand Down Expand Up @@ -33,8 +32,4 @@ app.use((err, req, res, next) => {
});
});

const listener = app.listen(config.app.port, () => {
logger.info(`Listening on port ${listener.address().port}`);
});

export default listener;
export default app;
7 changes: 0 additions & 7 deletions src/config/app.js

This file was deleted.

7 changes: 0 additions & 7 deletions src/config/index.js

This file was deleted.

9 changes: 4 additions & 5 deletions src/helper/logger.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint no-underscore-dangle: ["error", { "allow": ["_logger"] }] */

import winston from 'winston';
import config from './../config/index';

class Logger {
constructor() {
Expand All @@ -12,23 +11,23 @@ class Logger {
],
});

if (config.app.environment === 'local') {
if (process.env.NODE_ENV === 'local') {
this._logger.add(winston.transports.Console, {
level: 'info',
timestamp: () => (new Date().toISOString()),
formatter: options => (`${options.timestamp()} ${winston.config.colorize(options.level, options.level.toUpperCase())} ${(options.message ? options.message : '')} ${(options.meta && Object.keys(options.meta).length ? `\n\t${JSON.stringify(options.meta)}` : '')}`),
formatter: options => (`[${winston.config.colorize(options.level, options.level.toUpperCase())}][${options.timestamp()}] ${(options.message ? options.message : '')} ${(options.meta && Object.keys(options.meta).length ? `\n\t${JSON.stringify(options.meta)}` : '')}`),
});
}
}

info(message, meta) {
if (config.app.environment === 'local') {
if (process.env.NODE_ENV === 'local') {
this._logger.log('info', message, meta);
}
}

error(message, meta) {
if (config.app.environment === 'local') {
if (process.env.NODE_ENV === 'local') {
this._logger.log('error', message, meta);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ if (cluster.isMaster) {
cluster.fork();
});
} else { // Code to run if we're in a worker process
require('./app');
require('./server');
logger.info(`Worker ${cluster.worker.id} running!`);
}
15 changes: 15 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import http from 'http';
import normalizePort from 'normalize-port';
import app from './app';
import logger from './helper/logger';

const port = normalizePort(process.env.PORT || '3000');

/**
* Create HTTP server.
*/
const server = http.createServer(app).listen(port, () => {
logger.info(`Listening on port ${server.address().port}`);
});

export default server;
2 changes: 1 addition & 1 deletion test/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chai from 'chai';
import chaiHttp from 'chai-http';
import server from './../src/app';
import server from './../src/server';

const should = chai.should();

Expand Down
2 changes: 1 addition & 1 deletion test/controller/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chai from 'chai';
import chaiHttp from 'chai-http';
import server from './../../src/app';
import server from './../../src/server';

const should = chai.should();

Expand Down
2 changes: 1 addition & 1 deletion test/controller/v1/resource.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chai from 'chai';
import chaiHttp from 'chai-http';
import server from './../../../src/app';
import server from './../../../src/server';

const should = chai.should();

Expand Down

0 comments on commit 0e9e875

Please sign in to comment.