This document discusses building backend systems, apps, and internet of things solutions using Node.js and Google Cloud Platform. It begins with an overview of cloud computing and GCP before demonstrating a sample project that includes a Node.js backend using Google App Engine, mobile apps using React Native and Firebase, and an Alexa skill. It also covers using Datastore for storage, scheduling tasks with cron jobs, push notifications, and serverless functions.
11. 4.7
bilhões de
página
A Web é gigante hoje
Era do
Zetabyte*
* 1000 Exabytes
36.000
anos
de video
em hd
Últimos
20anos
http://www.livescience.com/54094-how-big-is-the-internet.html
11
24. O Google Cloud Platform é construído
na mesma infraestrutura que os
serviços do google rodam
• Rede Global
• Redundância
• Infraestrutura inovadora
24
33. Node.js
• Engine V8 do Google Chrome
• Rápido e escalável
• Orientado a eventos e não bloqueante
• Muito leve
• Perfeito para aplicações real-time
33
34. É apenas Javascript
• Navegador
• Server side
• Desktop - Electron e NW
• Mobile - Cordova, Ionic, React Native, etc.
• Embarcado - Mongoose OS e Espruino
• Geladeira, torradeira, etc
34
35. Ecossistema rico
• Muitas ferramentas feitas com Node
• Webpack ❤
• Gerenciadores de pacotes
• NPM e Yarn
• Editores
• Visual Studio Code, Atom, Webstorm
• Mais de 450 mil pacotes no npmjs.com
35
57. Serviço de Livros - Consulta
// The kind for the new entity
const kind = 'Book';
class BookService {
/* Codigo omitido */
async all() {
const query = datastore.createQuery(kind)
.order('date', { descending: true });
let results = await datastore.runQuery(query);
return results[0];
}
}
57
58. API de tarefa de busca e armazenamento de livros
module.exports = {
method: 'GET',
path: '/api/tasks/fetch-books',
config: {
handler: async (request, reply) => {
const crawler = new PackPubCrawler();
let books = await crawler.fetchBooksFromPacktPub();
const service = new BooksService();
let slug = service.getSlug(books.currentBook);
let exists = await service.exists(slug);
if (!exists) {
// Save new book
await service.save(books.currentBook);
//TODO: Notify clients that subscribed to this
}
reply({ books });
}
}
}
58
59. API de Livros
const BooksService = require('../BooksService');
module.exports = {
method: 'GET',
path: '/api/books',
config: {
handler: async (request, reply) => {
let service = new BooksService();
let books = await service.all();
reply({ books });
}
}
}
59
66. 1. Sintaxe do unix cron ou mais natural
2. Configure um cron.yaml
3. cloud app deploy cron.yaml
Google App Engine - cron.yaml
cron:
- description: fetch book every 30 mins
url: /api/tasks/fetch-books
schedule: every 30 mins
target: default
66
68. Google App Engine - Logs, monitoramento e trace
1. Pacote @google/cloud-trace
2. Just works
if (process.env.NODE_ENV === 'production') {
require('@google/cloud-trace').start();
}
68
81. Fluxo Alexa Custom Skill
Backend
Usuário
Alexa, Ask Packt Publishing
Fan what’s the book of the day
The free book of the day is title
Learning Raspberry Pi
Alexa Skill
HTTPS
81
82. Api de integração com a Alexa
const BooksService = require('../BooksService');
module.exports = {
method: 'POST',
path: '/api/alexa',
config: {
handler: async (request, reply) => {
let service = new BooksService();
let book = await service.getLastBook();
let message = `The free book of the day is titled $
{book.title}`;
reply({
"version": "1.0",
"sessionAttributes": {},
"response": {
"shouldEndSession": true,
"outputSpeech": {
"type": "SSML",
"ssml": `<speak>${message}</speak>`
},
}
});
}
}
} 82
83. PacktPub Notifier - Final
iOS e
Android
Amazon
Echo
Alexa
Custom
Skill
Packt
Publishing
Website
83