Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Javascript: Primeiros Passos
Search
Dmitry Rocha
July 18, 2015
Technology
87
0
Share
Javascript: Primeiros Passos
Meus primeiros passos em Javascript no backend.
Dmitry Rocha
July 18, 2015
More Decks by Dmitry Rocha
See All by Dmitry Rocha
Crystal Lang - Introduction
dmitryrck
0
140
Docker (for developers)
dmitryrck
0
210
Introdução a testes automatizados
dmitryrck
0
54
Introdução a Linguagem Crystal
dmitryrck
0
300
Docker
dmitryrck
0
74
Scoola Talk
dmitryrck
0
66
Testes, testes everywhere
dmitryrck
0
53
Saltos no Vim - GURUPI
dmitryrck
0
54
Other Decks in Technology
See All in Technology
Terraformモジュールは、なぜ「魔境」化するのか
hayama17
1
120
JEP 522 Deep Dive - G1 GC同期コスト削減によるスループット向上を徹底検証&解説
tabatad
1
420
TypeScript Compiler APIとPHP-Parserを活用し、TypeScriptとPHPで型を共有する
shuta13
0
240
ビジュアルプログラミングIoTLT vol.23
1ftseabass
PRO
0
160
ルールやカスタム機能、どう使う?理想の出力を引き出すために今知りたいIBM Bob 5つの機能
muehara
0
150
なぜハノーバーメッセに行くべきなのか 〜初参加だから語れること〜
tanakaseiya
0
180
大学生が本気でDatabricksを活用してDiscordサークルをデータ駆動させてみた
phantomjuju
1
300
個人AIからチームAIへ:開発における品質と生産性の再設計
moongift
PRO
0
320
コードレビューを制するチームがソフトウェアデリバリーのフローを制す / Beyond Code Review: Distributing Its Responsibilities Across the SDLC
mtx2s
3
480
GitHub Copilot CLIでWebアクセシビリティを改善した話
tomokusaba
0
140
プラットフォームエンジニア ワークショップ/ platform-workshop
databricksjapan
0
140
イベントで大活躍する電子ペーパー名札 〜その3〜 / ビジュアルプログラミングIoTLT vol.23
you
PRO
0
170
Featured
See All Featured
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
370
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
320
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
The Cost Of JavaScript in 2023
addyosmani
55
10k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
190
Build your cross-platform service in a week with App Engine
jlugia
234
18k
The browser strikes back
jonoalderson
0
1.1k
sira's awesome portfolio website redesign presentation
elsirapls
0
270
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
440
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
210
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
810
Transcript
Javascript: Primeiros Passos Dmitry Rocha GURUPI, PUG-PI, THC, … July
18, 2015 1 / 25
Mas primeiro: Javascript no backend E Primeiros passos 2 /
25
Meu projeto, minhas regras 1. Liberdade de escolha da tecnologia;
2. Esta parte do projeto: banco de dados somente leitura; 3. Servidor e banco de dados legado; 3 / 25
Eu não decidi fazer em javascript. 4 / 25
Eu fui… …marotamente tentando acessar o banco de dados via
js… 5 / 25
Eu fui… …marotamente tentando acessar o banco de dados via
js… …”vou renderizar uma notíciazinha aqui”… 6 / 25
Eu fui… …marotamente tentando acessar o banco de dados via
js… …”vou renderizar uma notíciazinha aqui”… …PUTZ: FIZ A PORRA TODA EM JS. 7 / 25
Javascript Básico + Testes 8 / 25
Javascript Básico + Testes $ npm init 9 / 25
Javascript Básico + Testes $ npm init $ npm install
mocha --save-dev 10 / 25
Javascript Básico + Testes $ npm init $ npm install
mocha --save-dev $ npm install expect.js --save-dev 11 / 25
Live coding ;) 12 / 25
Banco de Dados sequelizejs.com 13 / 25
Banco de Dados $ npm install sequelize 14 / 25
Live coding? Not today 1 var DataTypes = require('sequelize'); 2
var sequelize = new DataTypes('database', 'username', 3 'password', { 4 logging: false, port: 'port', host: 'host' 5 }); 6 var db = {}; 7 8 db.DataTypes = DataTypes; 9 db.sequelize = sequelize; 10 11 var news = sequelize['import'](__dirname + '/news'); 12 var section = sequelize['import'](__dirname + '/section'); 13 db.news = news; 14 db.section = section; 15 db.news.belongsTo(db.section, { foreignKey: 'secao_id' }); 16 17 module.exports = db; 15 / 25
Banco de Dados: ”model” 1 module.exports = function(sequelize, DataTypes) {
2 return sequelize.define('news', { 3 title: DataTypes.STRING, 4 content: DataTypes.TEXT, 5 status: DataTypes.INTEGER, 6 date: DataTypes.DATE, 7 'secao_id': DataTypes.STRING 8 }, { 9 tableName: 'noticias', 10 underscored: true, 11 timestamps: false, 12 scopes: { 13 'feed': { 14 where: { status: 1 }, 15 order: [ ['id', 'desc'] ], 16 limit: 20 17 }, 18 } 19 }); 20 }; 16 / 25
Banco de Dados: teste 1 process.env.NODE_ENV = 'test'; 2 var
expect = require('expect.js'); 3 describe('News', function(){ 4 before(function() { 5 this.db = require('../../models'); 6 7 this.news = { 8 title: 'Letters from Germany', slug: 'letters-from-germany', data: new Date() 9 }; 10 }); 11 12 beforeEach(function() { return this.db.news.destroy({ truncate: true }); }); 13 14 describe('create', function() { 15 it('should be persisted', function(done) { 16 return this.db.news.create(this.news).then(function(news) { 17 expect(news.isNewRecord).to.equal(false); 18 19 return done(); 20 }); 21 }); 22 23 it('has title', function(done) { 24 return this.db.news.create(this.news).then(function(news) { 25 expect(news.titulo).to.equal('Letters from Germany'); 26 27 return done(); 28 }); 29 }); 30 // … 31 }); 32 }); 17 / 25
Servidor Web expressjs.com 18 / 25
Servidor Web $ npm install express 19 / 25
Servidor Web: exemplo básico 1 var express = require('express'); 2
var app = express(); 3 4 app.get('/', function (req, res) { 5 res.send('Hello World!'); 6 }); 7 8 var server = app.listen(3000, function () { 9 var host = server.address().address; 10 var port = server.address().port; 11 12 console.log('Example app listening at http://%s:%s', host, port); 13 }); 20 / 25
Servidor Web: Gerador $ npm install express-generator -g 21 /
25
Servidor Web: Gerador $ npm install express-generator -g $ express
myapp --ejs 22 / 25
Servidor Web: Gerador $ npm install express-generator -g $ express
myapp --ejs Live showing, exemplo do gerador. 23 / 25
Mais informações no meu blog. Fiz duas postagens sobre uso
do sequelize com jasmine 1 e sequelize com mocha 2. 1Jasmine e Sequelize: Uma introdução - http://dmitryrck.github.io/jasmine-sequelize/ 2Mocha, expect.js e Sequelize: Uma introdução - http://dmitryrck.github.io/mocha-sequelize/ 24 / 25
25 / 25