Lecture 13 - Backend Development
Lecture 13 - Backend Development
Sparklines in NYT
http://www.nytimes.com/interactive/2016/upshot/presidential-polls-forecast.html
ith
c t sw
e r a
int
d
en
ont
e fr
t h “Back End”
Wha t Data storage
Some
Firebase
other API Some other logic
th
wi
c ts
a
i ter
n“Back
d End”
nt
en Data storage
fr o
the Some other
at Firebase Some other logic
Wh API
Frontend Backend
Pros Pros
Very responsive (low latency) Easy to refactor between multiple
clients
Logic is hidden from users (good for
Cons security, compatibility, and intensive
Security computation)
Performance Cons
Unable to share between front-ends Interactions require a round-trip to
server
LaToza/Bell GMU SWE 432 Fall 2016 6
Why Trust Matters
• Example: Transaction app
function updateBalance(user, amountToAdd)
{
user.balance = user.balance + amountToAdd;
fireRef.child(user.username).child("balance").set(user.balance);
}
• What’s wrong?
• How do you fix that?
“Back End”
Data storage
Some
Firebase
other API Some other logic
“Back End”
Data storage
Some Our own
Firebase
other API backend Some other logic
AJAX Connection to
Web “Front End”
Frontend
Logic
Persistent Data
web server
Runs a program
Give me /myApplicationEndpoint
HTTP Response
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
<html><head>...
Connection to
Frontend View
Logic Controller
Lecture 10
Dependencies
Configuration Declares what modules you need
Package
Manager Provides the modules to your app
app.listen(port, function () {
console.log('Example app listening on port' + port);
});
var express
1: Make = require(‘express');
a directory, myapp Creates a configuration file
Import the module express for your project
2: Enter that directory, type npm init (accept all defaults)
var app = express();
3: Type npma install express --save Tells NPM that you want to use
Create new instance of express
4: Create text file app.js: express, and to save that in your
var port = process.env.port || 3000; project config
Decide what port we want express to listen on
• For post:
app.post("/somePath", function(req, res){
//Read stuff from req, then call res.send(myResponse)
});
In our security lecture we’ll talk about having some data writable through
the web app directly and some only through node. For now, we’ll talk
about the simplest case: Only allow writes through our node backend.
LaToza/Bell GMU SWE 432 Fall 2016 27
Firebase + Node
• Step 1: Create a special access key for our Node app
to use to access our database
• This key will distinguish our node app from the web app
• Now you can keep publishing your API key, but have a
private key that you never publish publicly
• https://firebase.google.com/docs/server/setup
1 Create a Firebase project in the Firebase console, if you don't already have one. If you already have an existing Google project
associated with your app, click Import Google Project. Otherwise, click Create New Project.
2 Click settings and select Permissions.
3 Select Service accounts from the menu on the left.
4 Click Create service account.
a Enter a name for your service account. You can optionally customize the ID from the one automatically generated from
the name.
b Choose Project > Editor from the Role dropdown.
c Select Furnish a new private key and leave the Key type as JSON.
d Leave Enable Google Apps Domain-wide Delegation unselected.
e Click Create.