Introduzione a Fastify
Fastify è un web framework per Javascript e Node.js, con una struttura molto simile ad altri web framework, e con molti plugin a disposizione.
In questo articolo vediamo un esempio introduttivo.
Prima di tutto installiamolo con npm:
npm i fastify
Questo il mio index.js (o server.js, come volete):
import Fastify from 'fastify';
const fastify = Fastify({
logger: true
});
fastify.get('/', function (request, reply) {
reply.send({hello: 'world'});
});
fastify.get('/utente/:id', function (request, reply) {
const {id} = request.params;
reply.send({id: id});
});
fastify.get('/utente/:id/:token', function (request, reply) {
const {id, token} = request.params;
reply.send({id: id, token: token});
});
fastify.listen({port: 3000}, function (err, address) {
if (err) {
fastify.log.error(err);
process.exit(1);
}
});
Ho creato tre rotte:
- http://localhost:3000/
- http://localhost:3000/utente/10
- http://localhost:3000/utente/10/54564046406
Enjoy!
javascript npm nodejs fastify
Commentami!