Node.js Academy

Make a web service 👾

#ege-academy-nodejs @rbung @yagong

What is it For ?

Expose data, endpoints to trigger backend works

#ege-academy-nodejs @rbung @yagong

How ?

#ege-academy-nodejs @rbung @yagong

Why Express ? 1/2

#ege-academy-nodejs @rbung @yagong

Why Express ? 2/2

#ege-academy-nodejs @rbung @yagong

Express - Concepts

  • Routing
  • Middleware
  • Template engine

#ege-academy-nodejs @rbung @yagong

🙅‍♂️

Express - Routing

#ege-academy-nodejs @rbung @yagong

Routing refers to how an application’s endpoints (URIs) respond to client requests.

app.get('/', function (req, res) {
  res.send('hello world')
})

Express - Middleware

#ege-academy-nodejs @rbung @yagong

Middleware functions are functions that have access to the request object (req), the response object (res), and the next function in the application’s request-response cycle.

const myLogger = function (req, res, next) {
  console.log('LOGGED')
  next()
}

// ... some code

app.use(myLogger)

Express - Middleware

#ege-academy-nodejs @rbung @yagong

A lot's of useful middleware !

  • body-parser
  • cookie-parser
  • csurf
  • response-time

 

And more : https://expressjs.com/en/resources/middleware.html

How to test ?

#ege-academy-nodejs @rbung @yagong

SuperTest - Example

#ege-academy-nodejs @rbung @yagong

describe('GET /users', function() {
  it('responds with json', function() {
    return request(app)
      .get('/users')
      .set('Accept', 'application/json')
      .expect(200)
      .then(response => {
          assert(response.body.email, 'foo@bar.com')
      })
  });
});

#ege-academy-nodejs @rbung @yagong

Node.js academy - Make a web service

By Rodolphe BUNG

Node.js academy - Make a web service

  • 899