Release monitor backend - 2
ECA Node/React - June 2019
Plan
- axios
- mongoose
- refactor code
- test
Project structure review
Http call with axios
Http client - get base version
using axios
npm i axios --save
get example
using axios
const axios = require('axios')
axios
.get('https://www.egencia.com/flight-service/base/version')
.then(response => {
console.log(response.data)
})
post example
using axios
const axios = require('axios')
axios
.post('http://localhost:3000/services', {
env: 'maui',
service_name: 'flight-service',
})
.then(response => {
console.log(response.data)
})
Integrate in release-monitor
MongoDB with mongoose
mongoose
start mongo with docker
docker run -p 27017:27017 mongo:latest
mongoose
connect to mongo and save
- connect
- define schema model (collection in MongoDB)
- add object
const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost:27017/database', {
useNewUrlParser: true,
})
const baseVersionSchema = new mongoose.Schema({
service_name: String,
build_version: String,
})
const BaseVersionModel = mongoose.model('BaseVersion', baseVersionSchema)
const baseVersion = new BaseVersionModel({
service_name: 'flight-service',
build_version: '2019',
})
baseVersion.save().then(() => {
console.log('saved')
})
mongoose
connect to mongo and get
- connect
- define schema model (collection in MongoDB)
- find
const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost:27017/database', {
useNewUrlParser: true,
})
const baseVersionSchema = new mongoose.Schema({
service_name: String,
build_version: String,
})
const BaseVersionModel = mongoose.model('BaseVersion', baseVersionSchema)
BaseVersionModel.find()
.lean() // necessary for json output
.then(data => {
console.log(data)
})
Integrate in release-monitor
let's refactor
- app.js: configure express
- index.js: configure the server
- src: src files
- routers: business logics to each route
- database: related to DB
test
npm i --save-dev jest supertest
npm i --save-dev eslint-plugin-jest
install dependencies
test
// in package.json add this section
"jest": {
"testEnvironment": "node"
}
// in .eslintrc.js, add plugins:
'plugin:jest/recommended'
configure jest
test with supertest
const app = require('../src/app')
const request = require('supertest')
it('should return hello world', async function() {
const response = await request(app).get('/hello-world')
expect(response.text).toEqual('hello world')
})
helloworld
test with mongodb-memory-server
npm install mongodb-memory-server --save-dev
const { MongoMemoryServer } = require('mongodb-memory-server')
const app = require('../../src/app')
const request = require('supertest')
const mongoose = require('mongoose')
let mongod
beforeEach(async () => {
mongod = new MongoMemoryServer()
const uri = await mongod.getConnectionString()
await mongoose.connect(uri)
})
afterEach(async () => {
mongod.stop()
})
test with nock
npm install nock --save-dev
nock('https://www.egencia.com')
.get('/flight-service/base/version')
.reply(200, {
application_name: 'flight-service',
active_profiles: 'prod,prodch,ancillaries,oms',
stage: 'prod',
machine_name: 'chexjvgect004',
application_data: {
step_bom_version: '8.6.2',
},
build_version: '2019-05-27-15-15-94093f32a',
build_number: '94093f32a449b31104c7c2767ee5fd2a42447b99',
build_date: '2019-05-27T15:17:025Z',
})
.persist()
add more tests
- test on POST /services
- test on GET /services/:env
FullstackJS Academy - course 5
By yagong
FullstackJS Academy - course 5
- 683