Release monitor backend - 1

ECA Node/React - June 2019

Plan

  1. webservice with express
  2. middleware
  3. asynchronous

Project structure review

create a server

Not this way

// server.js
var http = require('http');

//create a server object:
http.createServer((req, res) => {
  res.write('Hello World!'); //write a response to the client
  res.end(); //end the response
}).listen(8080); //the server object listens on port 8080

With Express

Install Express

npm i express --save

Start with hello world

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => res.send('Hello World!'))

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

Design the API

Design the API

  • GET /services/{env}
  • POST /services
{
  "env": "maui",
  "service_name": "service-name"
}
[{
  "service_name": "service-name",
  "build_version": "2019-01-01-01-01-1234"
}]

let's go

want hot reloading?

npm i nodemon --save-dev

// package.json
"scripts": {
    "local:dev": "nodemon index.js"
  }

middleware and routes

let's refactor

app.js

index.js

src

routers

asynchronous

callback

setTimeout/setInterval

promise

async/await

process.nextTick

How NodeJS handle asynchronous

eventloop

How NodeJS handle asynchronous

eventloop

Quiz

mix callback,promise,async/await,setTimeout

Http client - get base version

using axios

npm i axios --save

https://floobits.com/yagong/backend

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')
})

Text

mongoose

FullstackJS Academy - course 4

By yagong

FullstackJS Academy - course 4

  • 781