Skip to content

Commit

Permalink
Fixed the issue of broken when not given the default parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
yunnysunny committed Sep 17, 2018
1 parent a1d460a commit e0f3574
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v0.1.1
## Fix
1. Fixed the issue of broken when not given the default parameter.

# v0.1.0
## Fix
1. Fixed the code error in `readme.md` .
Expand Down
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ const serverIp = require('ip').address();
const pid = process.pid;
/**
* @module req-log
* @param {Object=} kafkaSchedule The instance of class KafkaProducer from the package of [queue-schedule](https://npmjs.com/package/queue-schedule).
* @param {Object=} alarm The alarm object, it should has the function of sendAll.
* @param {Object} options
* @param {Object=} options.kafkaSchedule The instance of class KafkaProducer from the package of [queue-schedule](https://npmjs.com/package/queue-schedule).
* @param {Object=} options.alarm The alarm object, it should has the function of sendAll.
*/
module.exports = function({kafkaSchedule,alarm}) {
module.exports = function(options={}) {
const {kafkaSchedule=null,alarm=null} = options;
return function(req, res, next) {
//记录请求时间
const req_time = Date.now();
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "@yunnysunny/request-logging",
"version": "0.1.0",
"version": "0.1.1",
"description": "Print the express request log to console and save it to kafka when required, and even can send alram message when the response code greater than 500.",
"main": "index.js",
"scripts": {
"test": "nyc mocha --recursive test/mocha --timeout 999999 --exit",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"doc": "jsdoc2md index.js index.js > doc/api.md"
"doc": "jsdoc2md index.js index.js > doc/api.md",
"release" : "git push && release-to-github-with-changelog"
},
"repository": {
"type": "git",
Expand Down
53 changes: 53 additions & 0 deletions test/express/src/app-no-param.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');

const routes = require('./routes/index');

const {
slogger,
port,
} = require('./config');
const requestLog = require('../../../index');

const app = express();
app.enable('trust proxy');

// view engine setup
app.set('port', port);
app.use(requestLog({}));

app.use(bodyParser.json({limit: '1mb'}));
app.use(bodyParser.urlencoded({
extended: false,
limit: '1mb'
}));


app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
const err = new Error('Not Found:' + req.path);
err.status = 404;
next(err);
});

// error handlers
app.use(function(err, req, res, next) {
const status = err.status;
if (status === 404) {
return res.status(404).send(err.message || '未知异常');
}
res.status(status || 500);
slogger.error('发现应用未捕获异常', err);
res.send({
msg: err.message || '未知异常',
code: 0xffff
});
});


module.exports = app;
16 changes: 16 additions & 0 deletions test/mocha/no_param_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const app = require('../express/src/app-no-param');

const request = require('supertest');
describe('no session test:',function() {
it('sucess when request / ok',function(done) {
request(app)
.get('/')
.expect(200)
.end(function(err) {
if (err) {
return done(err);
}
done();
});
});
});

0 comments on commit e0f3574

Please sign in to comment.