@WellsSA
@WellsSA
@WellsSA
@WellsSA
@WellsSA
@WellsSA
@WellsSA
Como
O quê
@WellsSA
@WellsSA
@WellsSA
@WellsSA
@WellsSA
@WellsSA
@WellsSA
@WellsSA
50k+ GitHub stars
@WellsSA
@WellsSA
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Olá Mundo\n');
}).listen(8000, '127.0.0.1');
console.log('Servidor executando em http://127.0.0.1:8000/');
var express = require('express');
var app = express();
app.get('/', function(req, res) {
res.send('Olá Mundo!');
});
app.listen(3000, function() {
console.log('Rodando!');
});
Exemplo da developer.mozilla.org
usando o módulo http do node:
usando o express:
@WellsSA
@WellsSA
@WellsSA
request
response
(emit)
(emit)
(listen)
(listen)
(emit)
(listen)
@WellsSA
@WellsSA
50k+ GitHub stars
@WellsSA
Do próprio site do Socket.io
req
res
(emit)
(listen)
POST: /my-route
http://
ws://
ws://
@WellsSA
@WellsSA
@WellsSA
85k+ GitHub stars
@WellsSA
@WellsSA
@WellsSA
@WellsSA
(Object Relational Mapper)
*A classificação de ORMs como bibliotecas ou frameworks é controversa, mas independente disso, eles são úteis!
(Object Document Mapper)
@WellsSA
29k+ GitHub stars
@WellsSA
@WellsSA
const Koa = require('koa');
const koaBody = require('koa-body');
const router = require('@koa/router')();
const app = module.exports = new Koa();
app.use(koaBody({
jsonLimit: '1kb'
}));
router.get('/', async function(ctx) {
const body = ctx.request.body;
if (!body.name) ctx.throw(400, '.name required');
ctx.body = { name: body.name.toUpperCase() };
});
app.use(router.routes());
if (!module.parent) app.listen(3000);
@WellsSA
const http = require('http');
const https = require('https');
const Koa = require('koa');
const app = new Koa();
http.createServer(app.callback()).listen(3000);
https.createServer(app.callback()).listen(3001);
@WellsSA
30k+ GitHub stars
@WellsSA
@WellsSA
@WellsSA
21k+ GithHub stars
@WellsSA
Por padrão: Express, Socket.io, Waterline (ORM)
Com a proposta de fazer:
Serem mais fáceis que nunca
@WellsSA
@WellsSA
module.exports = {
friendlyName: 'Welcome user',
description: 'Look up the specified user and welcome them, or redirect to a signup page if no user was found.',
inputs: {
userId: {
description: 'The ID of the user to look up.',
// By declaring a numeric example, Sails will automatically respond with `res.badRequest`
// if the `userId` parameter is not a number.
type: 'number',
// By making the `userId` parameter required, Sails will automatically respond with
// `res.badRequest` if it's left out.
required: true
}
},
exits: {
success: {
responseType: 'view',
viewTemplatePath: 'pages/welcome'
},
notFound: {
description: 'No user with the specified ID was found in the database.',
responseType: 'notFound'
}
},
fn: async function ({userId}) {
// Look up the user whose ID was specified in the request.
// Note that we don't have to validate that `userId` is a number;
// the machine runner does this for us and returns `badRequest`
// if validation fails.
var user = await User.findOne({ id: userId });
// If no user was found, respond "notFound" (like calling `res.notFound()`)
if (!user) { throw 'notFound'; }
// Display a personalized welcome view.
return {
name: user.name
};
}
};
@WellsSA
41k+ GitHub stars
@WellsSA
@WellsSA
@WellsSA
@WellsSA
12k+ GitHub stars
@WellsSA
13k+ GitHub stars
@WellsSA
@WellsSA
@WellsSA
@WellsSA
Frameworks simples facilitam o estudo
Frameworks complexos facilitam o trabalho em equipe
@WellsSA
@WellsSA
@WellsSA
@WellsSA
@WellsSA