-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0f3574
commit 6f2737a
Showing
10 changed files
with
181 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
language: node_js | ||
|
||
services: | ||
- mongodb | ||
|
||
before_install: | ||
- jdk_switcher use oraclejdk8 | ||
- java -version | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const express = require('express'); | ||
const path = require('path'); | ||
const bodyParser = require('body-parser'); | ||
|
||
const routes = require('./routes/index'); | ||
|
||
const { | ||
slogger, | ||
port, | ||
requestLogModel | ||
} = require('./config'); | ||
const requestLog = require('../../../index'); | ||
|
||
const app = express(); | ||
app.enable('trust proxy'); | ||
|
||
// view engine setup | ||
app.set('port', port); | ||
app.use(requestLog({mongooseModel:requestLogModel})); | ||
|
||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const {Schema} = require('mongoose'); | ||
|
||
const requestLogSchema = new Schema({ | ||
req_time: Date | ||
},{ | ||
autoIndex: false, | ||
timestamps: { | ||
createdAt: 'created_at', | ||
updatedAt : 'update_at' | ||
}, | ||
strict: false | ||
}); | ||
|
||
|
||
|
||
module.exports = requestLogSchema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const app = require('../express/src/app-with-mongodb'); | ||
|
||
const request = require('supertest'); | ||
const {expect} = require('chai'); | ||
const rand = Math.random(); | ||
const reqUrl = '/?rand='+rand; | ||
const {requestLogModel} = require('../express/src/config'); | ||
describe('mongodb test:',function() { | ||
it('sucess when request / ok',function(done) { | ||
request(app) | ||
.get(reqUrl) | ||
.expect(200) | ||
.end(function(err) { | ||
if (err) { | ||
return done(err); | ||
} | ||
done(); | ||
}); | ||
}); | ||
it('the lastest url is ' + reqUrl,function(done) { | ||
requestLogModel.findOne({},{original_url:1},{ | ||
sort:{_id:-1},lean:true | ||
},function(err,item) { | ||
if (err) { | ||
return done(err); | ||
} | ||
if (!item) { | ||
return done('save to mongo failed'); | ||
} | ||
expect(item.original_url).equal(reqUrl); | ||
done(); | ||
}); | ||
}); | ||
}); |