+ All Categories
Home > Documents > Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the...

Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the...

Date post: 24-May-2020
Category:
Upload: others
View: 4 times
Download: 0 times
Share this document with a friend
65
Server-side JavaScript C. Dallago, T. Goldberg, D. Nechaev, B. Rost, D. Schwartz, S. Wilzbach and G. Yachdav Technische Universität München Faculty of Informatics Chair for Bioinformatics
Transcript
Page 1: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

Server-side JavaScript

C. Dallago, T. Goldberg, D. Nechaev, B. Rost,

D. Schwartz, S. Wilzbach and G. Yachdav

Technische Universität München

Faculty of Informatics Chair for Bioinformatics

Page 2: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

• MSc Informatics student • Background in software engineering • Originally italian • Love to travel • Like to think of myself as a full-stack (front- / backend) JavaScript developer

2

About myself

Page 3: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

• Node.js basics • What you need to build your first back-end • First hands-on experience with our example

What we will learn in this session:

Page 4: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

http://example.dallago.us/

What’s at the root of example.dallago.us?

An index (index.html)

Requests

4

Page 5: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

The index needs script.js and image.png!

Requests II

5

http://example.dallago.us/script.js

index.html

http://example.dallago.us/image.png

script.js image.png

Page 6: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

Requests III

6

<img src=“http://example.dallago.us/image.png”>

image.png

Page 7: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

Requests IV

7

<img src=“image.png”>

image.png

Page 8: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

8

Types of requests

• GET: Used to get/retrieve resources • POST: Create and update resources (what web-forms do) • PUT: Update and create resources (given an URL) • DELETE: Delete resources • … And more…

Page 9: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

https://cell.map/assets/images/protein.png

Protocol

Host name

Location/Path

Resource

URLs

9

Page 10: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

10.129.30.145

/ assets images

https://cell.map/ assets/ images/ protein.png

protein.png

URLs II

10

Page 11: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

Up until now: - JavaScript for the front-end - Interpret JavaScript in the browser’s console

Next: - JavaScript for back-end development - Interpret JavaScript as an ordinary computer program - Use JavaScript to answer requests from the web

11

But what does JS have to do with all this?

Page 12: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

12

On a side note…

Page 13: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

1. How do you load a script in HTML?

2. What request does a browser perform when reading an <img> tag?

3. What is the difference between PUT and POST requests?

Question time:

13

Page 14: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

1. How do you load a script in HTML?

2. What request does a browser perform when reading an <img> tag?

3. What is the difference between PUT and POST requests?

Question time:

14

1. Using a <script src=“..”> tag

2. A GET request

3. PUT is used mainly for update and needs specific URL

Page 15: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

15

Page 16: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

- JavaScript interpreter - Single-core, ~1.8GB RAM - Same language and concepts as for the front-end (event-based, asynchronous) - Provides various libraries, for example to read/write files - You can build:

- Command-line scripts - APIs - Web applications with back- and front-end 16

Page 17: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

17

Asynchronous, non-blocking executionTraditional approach:

Node.js’s approach:

GET: File

GET: Data

getFile

getData

open

connect to DB

read

query

send

send

GET: File

GET: Data

Node V8 Engine

Event queue

(getFile(file), open(file)) (getData(db), connect(db))

(open(file), read(file)) (connect(db), query(db))

(read(file), send(file)) (send(file), none)

(query(db), send(db)) (send(db), none)

Function Callback

Page 18: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

- Node Package Manager (NPM) - Provides a large number of packages for:

- Front-end - Back-end - Scripting

- Imagine node.js to be your smartphone and NPM to be your app store. - As every app store: some apps are good, some are not (look at documentation!). 18

Page 19: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

19

NPM growth

http://www.modulecounts.com

Page 20: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

name,surname,age Christian,Dallago,23 Alex,Schmidt,25

20

ParsJS → https://www.npmjs.com/package/parsjs Lets you transform a comma separated values file into an array of JSON objects:

[{ "name": "Christian", "surname": "Dallago", "age": "23" }, { "name": "Alex", "surname": "Schmidt", "age": "25" }]

Node.js as a scripting language

Page 21: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

21

• JSON is a data exchange format widely used in the web • A JSON object looks like this:

{ "name": "Christian", "surname": "Dallago", "age": "23" }

The JavaScript Object Notation (JSON)

Page 22: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

22

In addition to Strings, JSON objects can contain all the primitive JS data types and compound type:{ "type": "car", "price": 30440.32, "currency": "eur", "available": true, "locations": ["it", "es", "fr"], "reference": { "name": "Christian", "phone": "+3932984753" } }

The JavaScript Object Notation (JSON) II

Page 23: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

In a node.js application, the NPM dependencies and the description of the application are in a file called package.json

23

Defining dependencies to NPM package using package.json

{ "name": "example", "version": "1.0.0", "description": "Example", "main": "index.js", "author": "Christian Dallago <[email protected]>", "license": "ISC", "dependencies": { "express": "^4.10.1", "pug": "latest", "mongoose": "latest" } }

Page 24: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

24

Installing NPM packages using command line

$ npm install --global parsjs

Page 25: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

1. What can you use node for?

2. What can you store in a JSON object?

3. Where do you define dependencies in a node application?

Question time:

25

Page 26: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

1. What can you use node for?

2. What can you store in a JSON object?

3. Where do you define dependencies in a node application?

Question time:

26

1. Scripting, building backends and full web-apps

2. All JavaScript primitive types and compound types

3. In the “dependencies” section of the package.json

Page 27: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

27

Web-application components with node.js

Page 28: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

28

Node.js as back-end - Request handling

• Express.js: de facto standard • koa: simplified version of Express.js • Restify: focus on REST APIs and based on Express.js

Page 29: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

• SQLite, PostgreSQL or MySQL: Relational databases • MongoDB, CouchDB: JSON* document stores, a non-relational database type • Redis, LevelDB: Key-value stores, another non-relational database type

29

Node.js as back-end - Databases

Page 30: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

30

Node.js as back-end - View EnginesNot needed if you build a REST API

• Angular.js: backed by Google, part of the Mongo+Express+Angular+Node (MEAN) stack • React: developed by Facebook • Pug (previously Jade): powerful and simple templating engine • Ember: another choice among small communities of developers

Page 31: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

1. Why would you use a non-relational database to prototype?

2. In what order do you need components to build your application?

Question time:

31

Page 32: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

1. Why would you use a non-relational database to prototype?

2. In what order do you need components to build your application?

Question time:

32

1. No schema, no relations, simple to conceptualize

1. Request handler 2. Database 3. View Engine

Page 33: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

33

An (actually; two) example application(s)

Page 34: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

34

+ + +

Node.js as back-end in our example

Page 35: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

35

+ + +

Node.js as back-end in our example II

XViews are going to be explained in the next session. Let’s focus on building an API.

Page 36: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

First code example (API): http://example.dallago.us/public/api.zip Complete code example: http://example.dallago.us/public/complete.zip

Running example: http://example.dallago.us

The example uses the well known Model View Controllers (MVC) pattern:

• Models (the data objects), • Views (the way this data is presented) and • Controllers (the logic that combines data and calculations to produce meaningful Views).

36

Reference examples

Page 37: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

37

First example folder structure

/ appcontrollers

relationships.jsmodels

relationships.jsviews

config.jsondatabase.jsindex.jspackage.jsonrouter.js

FoldersServer-side JavaScript

Page 38: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

38

Complete example folder structure/ app

controllersrelationships.jsfrontend.js

modelsrelationships.js

viewsbase.pughome.pugnavigation.pugvisualization.pug

publiclibs

d3.jsvisualization

script.jsstyle.css

global.cssconfig.jsondatabase.jsindex.jspackage.jsonrouter.js

FoldersServer-side JavaScriptViewsFront-end JavaScriptFront-end Style (CSS)

Page 39: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

How to get the examples running

1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( https://nodejs.org ) 4. Use the command-line to navigate to the folder where index.js is 5. Run the command npm install (this will install the dependencies and create a new

folder in the current directory called “node_modules”) 6. Run the command node index.js 7. Navigate from your browser to http://localhost:3000/api/relationships

39

Page 40: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

40

Controllers and Models

Page 41: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

41

- Framework that allows to react on incoming requests and send some response - Provide extendable middle wares and layers - Can be extended through other packages

Page 42: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

42

Express.js in our first example application

/ appcontrollers

relationships.jsmodels

relationships.jsviews

config.jsondatabase.jsindex.jspackage.jsonrouter.js

Page 43: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

43

const express = require('express'); const app = express();

// Set 'port' value to either an environment value PORT or 3000 app.set('port', process.env.PORT || 3000);

// Create router var router = require(”./router”);

// Router listens on / (root) app.use('/', router);

// Start listening for requests on the port defined earlier app.listen(app.get('port'), function(){ console.log("Express server listening on port " + app.get('port')); });

Express.js: index.js

Page 44: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

44

// API router.get("/api/relationships", relationshipsController.relationships);

relationships: function(request, response){ relationshipsModel.find(function(error, results){ if(error){ response.status(500).send(error); } else { response.status(200).send(results); } }); }

Express.js: router.js + controllers/relationships.js

Page 45: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

45

- Document-oriented database management system - Non-relational system - Stores BSON, that is: Binary-JSON - Can store images, music, as well as ordinary JSON objects - Out of the box it doesn’t enforce a schema

Page 46: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

46

JSON:{ "name": "Christian", "surname": "Dallago", "age": "23" }

MongoDB entry:{ "_id": { "$oid": "57657fe0dcba0f1e46d2aa4b" }, "name": "Christian", "surname": "Dallago", "age": "23" }

MongoDB

Page 47: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

47

- To connect to a mongo database you need a “driver” - Allows you to:

- Define Models (enforce some type of schema) - Perform queries (find, create, update, remove items) - Connect to databases and handle connection events

Page 48: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

48

Mongoose in our first example application

/ appcontrollers

relationships.jsmodels

relationships.jsviews

config.jsondatabase.jsindex.jspackage.jsonrouter.js

Page 49: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

49

const mongoose = require('mongoose');

const relationshipsModel = mongoose.model('relationships', { source: { type: String, require: true }, target: { type: String, require: true }, weight: { type: Number, require: true } });

module.exports = relationshipsModel;

Mongoose: models/relationships.js (Model)

Page 50: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

50

const relationshipsModel = require('../models/relationships'));

module.exports = { relationships: function(request, response){ relationshipsModel.find(function(error, results){ if(error){ response.status(500).send(error); } else { response.status(200).send(results); } }); } }

Mongoose: controllers/relationships.js (Model find)

Page 51: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

51

Mongoose: What else can you do?

You can store new items in the database

relationshipsModel.create({ source: "me", target: "you", weight: 6 }, function(error, newItem){ console.log("Created:", newItem); });

Page 52: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

52

Mongoose: What else can you do?

You can remove items from the database

relationshipsModel.findOneAndRemove({ source: "me" }, function(error, removedItem){ console.log("Removed:", removedItem); });

Page 53: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

53

Mongoose: What else can you do?

You can update items in the database

relationshipsModel.update({ source: "me" }, { $set: { target: "Tommen" } });

Will update only the first document matching the query

Page 54: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

54

Mongoose: CRUD

What we have just seen has been the CRUD stack: • Create → Model.create() • Read → Model.find() • Update → Model.update() • Delete → Model.remove()

Page 55: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

1. Why do you pass the prototype of a function to a request handler?

2. Do you have to implement the CRUD stack in your application?

Question time:

55

Page 56: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

1. Why do you pass the prototype of a function to a request handler?

3. Do you have to implement the CRUD stack in your application?

Question time:

56

1. Because we want the function to be executed every time a request comes, and not service the output of one function call (on startup) for every request.

2. Depends!!

Page 57: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

57

Views

Page 58: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

58

- Ia view engine - Gives us a way to define reusable and extendable HTML components - Imagine your web page as a set of pieces: navigation, menus, content, footer,… - Uses a simplified syntax - Can use control statements (like each and if) and variables passed to the view

Page 59: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

59

Pug: Where do you find it?

/ appcontrollers

relationships.jsfrontend.js

modelsrelationships.js

viewsbase.pughome.pugnavigation.pugvisualization.pug

publiclibs

d3.jsvisualization

script.jsstyle.css

global.cssconfig.jsondatabase.jsindex.jspackage.jsonrouter.js

Page 60: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

Pug: home.pug + base.pug + navigation.pug

60

html head title= title link(rel='stylesheet', href='/public/style.css') meta(charset="utf-8") body .wrapper include navigation block content

extends base

block content h1 Here is a title p A nice Paragraph

ul li a(href="/")Home

Page 61: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

<html> <head> <title>Home</title> <link rel="stylesheet" href="/public/style.css"/> <meta charset="utf-8"/> </head> <body> <div class="wrapper"> <ul id="navigation"> <li><a href="/">Home </a></li> </ul> <h1>Here is a title</h1> <p>A nice Paragraph</p> </div> </body> </html>

Pug: home.pug + base.pug + navigation.pug

61

Page 62: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

1. What are viewes used for?

2. What is the difference between extending and importing?

3. Give me two examples of applications in biology where you need views and two where you don’t need them

Question time:

62

Page 63: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

1. What are viewes used for?

3.What is the difference between extending and importing?

Question time:

63

1. Present data on the web. Design your application using reusable components. Extend HTML’s shortcomings.

2. Extend: You extend part of existing code at a specific location.

3. Import: You take all the code in the file and paste it in that location.

Page 64: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

64

Question time

Page 65: Server-side JavaScript - Rostlab · 2016-07-01 · How to get the examples running 1. Download the zip files 2. Unzip the files 3. Make sure you have node.js and NPM installed ( )

65

Demo time


Recommended