Extension Rest Client

Créer un fichier app.js

const express = require('express');
const bodyParser = require('body-parser');
const app = express()

app.use(bodyParser.json());


const port = 3000

sayhello = function(pers){
    return `hello ${pers}`
}

app.post('/', (req, res) => {
  res.send(sayhello(req.body && req.body.pers || 'hi'))
})


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

Mettre en place l'application :
  • npm init -y
  • npm install express
Dans un terminal lancez l'application :
  • node app.js


Ajouter l'extension Rest API.

Créer un fichier api.http

POST http://localhost:3000
Content-Type: application/json

{"pers":"denis"}

En cliquant sur le lien Send Request (pas sur http://localhost:3000), on obtient la réponse suivante.



Plus besoin de Postman pour ce type de test.


Autre exemple avec une authentification 







ES6 module in nodejs

 https://stackoverflow.com/questions/46745014/alternative-for-dirname-in-node-js-when-using-es6-modules/53826979#53826979