Loading
Benoît Chanclou
This is a live streamed presentation. You will automatically follow the presenter and see the slide they're currently on.
facilement
Un serveur doit gérer
créer un serveur d'API
var http = require('http');
http.createServer(function(req, res) {
res.write('hello world');
res.end();
}).listen(3000);$ node mon_serveur.js
$ curl http://localhost:3000
hello worldvar http = require('http');
http.createServer(function(req, res) {
if (req.url == '/tom'){
res.write('hello tom')
} else {
res.write('hello world');
}
res.end();
}).listen(3000);$ node mon_serveur.js &
$ curl http://localhost:3000/
hello world
$ curl http://localhost:3000/tom
hello tom
$ curl http://localhost:3000/joe
hello world
$ npm install --dev express
ou
$ yarn add expressvar http = require('http');
var express = require('express');
var app = express();
app.use('/tom', (req, res, next) => {
res.write('hello tom');
res.end();
});
app.use((req, res, next) => {
res.write('hello world');
res.end();
});
http.createServer(app).listen(3000);
app.use([path,] callback [, callback...])
function mw1(req, res, next) { next(); }
app.get('/tom', (req, res, next) => {
res.write('hello tom');
res.end();
});
app.post('/tom', (req, res, next) => {
res.write('creating tom...');
res.end();
});
$ curl http://localhost:3000/tom
hello tom
$ curl -X POST http://localhost:3000/tom
creating tom...function mw1(req, res, next) { next(); }
Recherche d'une callback
appel callback
appel next
renvoyer la réponse
oui
non
non
oui
méga callback
Authentification
Extraction des paramètres
Récupération du tenant
Formater la réponse
Accès aux données
express.json([options])
express.static(root, [options])