Tuesday, January 13, Backend III: Node.js with Databases

Similar documents
MongoDB. Robert M. Vunabandi

welcome to BOILERCAMP HOW TO WEB DEV

RESTful APIs ECS 189 WEB PROGRAMMING. Browser s view. Browser s view. Browser s view. Browser s view. Which will It be for photobooth?

Human-Computer Interaction Design

Front End Programming

Review. Fundamentals of Website Development. Web Extensions Server side & Where is your JOB? The Department of Computer Science 11/30/2015

Express.JS. Prof. Cesare Pautasso Modularity

Persistence & State. SWE 432, Fall 2016 Design and Implementation of Software for the Web

MEAN February. techdt.la

Kickoff and Theme Reveal IAP 2017

CSC 443: Web Programming

Backend IV: Authentication, Authorization and Sanitization. Tuesday, January 13, 15

MongoDB Web Architecture

Persistence. SWE 432, Fall 2017 Design and Implementation of Software for the Web

Databases/JQuery AUGUST 1, 2018

Quick.JS Documentation

Lecture 14. Moving Forward 1 / 23

PROBLEMS IN PRACTICE: THE WEB MICHAEL ROITZSCH

Human-Computer Interaction Design

Topic 12: Connecting Express and Mongo

Backend Development. SWE 432, Fall 2017 Design and Implementation of Software for the Web

Welcome to CS50 section! This is Week 10 :(

Simple AngularJS thanks to Best Practices

a Very Short Introduction to AngularJS

CS193X: Web Programming Fundamentals

Tools. SWE 432, Fall Design and Implementation of Software for the Web

Catbook Workshop: Intro to NodeJS. Monde Duinkharjav

Extra Notes - Data Stores & APIs - using MongoDB and native driver

CS142 - Web Applications

Full Stack boot camp

Backend Development. SWE 432, Fall Web Application Development

Javascript Performance in the Browser. Charlie Fiskeaux II User Interface Engineer

What is Grails4Notes(TM)?

We are assuming you have node installed!

Web Server Setup Guide

Node.js. Node.js Overview. CS144: Web Applications

Storage Tier. Mendel Rosenblum. CS142 Lecture Notes - Database.js

Unifer Documentation. Release V1.0. Matthew S

MEAN Stack. 1. Introduction. 2. Foundation a. The Node.js framework b. Installing Node.js c. Using Node.js to execute scripts

What is Node.js? Tim Davis Director, The Turtle Partnership Ltd

CS193X: Web Programming Fundamentals

Lab 1 - Introduction to Angular

Security. CSC309 TA: Sukwon Oh

Client Side JavaScript and AJAX

Lab 4: create a Facebook Messenger bot and connect it to the Watson Conversation service

CS 142 Final Examination

MongoDB. CSC309 TA: Sukwon Oh

MongoDB w/ Some Node.JS Sprinkles

Homework 8: Ajax, JSON and Responsive Design Travel and Entertainment Search (Bootstrap/Angular/AJAX/JSON/jQuery /Cloud Exercise)

20486-Developing ASP.NET MVC 4 Web Applications

Server-Side JavaScript auf der JVM. Peter Doschkinow Senior Java Architect

JavaScript Rd2. -Kyle Simpson, You Don t Know JS

IN4MATX 133: User Interface Software

John Coggeshall Copyright 2006, Zend Technologies Inc.

WEB SECURITY WORKSHOP TEXSAW Presented by Solomon Boyd and Jiayang Wang

Sessions. Mendel Rosenblum. CS142 Lecture Notes - Sessions

Credits: Some of the slides are based on material adapted from

END-TO-END JAVASCRIPT WEB APPS

Developing Applications with Alfresco s Unified REST APIs. Will Abson, Alfresco

Unit 3 - Week 2 lectures: Building your webapp

Outline. 1. Load Testing 2. Frontend Tips 3. Server-side Tips

Offline-first PWA con Firebase y Vue.js

Smashing Node.JS: JavaScript Everywhere

Server execution of JavaScript: What could possibly go wrong?

Introduction to GraphQL and Relay. Presenter: Eric W. Greene

IBMi in the IT infrastructure of tomorrow

EPHP a tool for learning the basics of PHP development. Nick Whitelegg School of Media Arts and Technology Southampton Solent University

Basic Uses of JavaScript: Modifying Existing Scripts

HTML Advanced Portlets. Your Guides: Ben Rimmasch, Rahul Agrawal

Upload to your web space (e.g., UCSC) Due this Thursday 4/8 in class Deliverable: Send me an with the URL Grading:

NoSQL: NoInjections or NoSecurity

TOP DEVELOPERS MINDSET. All About the 5 Things You Don t Know.

Getting Mean. Practical Perspective. Prof. Dr. Alejandro Zunino, Prof. Dr. Alfredo Teyseyre. ISISTAN Department of Computer Science UNICEN

Enterprise Software Architecture & Design

Frontend II: Javascript and DOM Programming. Wednesday, January 7, 15

Introduction and first application. Luigi De Russis. Rails 101

Quick housekeeping Last Two Homeworks Extra Credit for demoing project prototypes Reminder about Project Deadlines/specifics Class on April 12th Resul

XML. Jonathan Geisler. April 18, 2008

Introduction to Express.js. CSC309 Feb. 6, 2015 Surya Nallu

Web Development for Dinosaurs An Introduction to Modern Web Development

Instructor s Notes Web Data Management Web Client/Server Concepts. Web Data Management Web Client/Server Concepts

Now go to bash and type the command ls to list files. The unix command unzip <filename> unzips a file.

CS 161 Computer Security

Courslets, a golf improvement web service. Peter Battaglia

Controller/server communication

Agile Web Development with Rails 5.1

Project Avatar: Server Side JavaScript on the JVM GeeCon - May David Software Evangelist - Oracle

Mavrig. a Tcl application construction kit. Jean-Claude Wippler Equi 4 Software, NL. EuroTcl 2008, Strasbourg, FR

Job Description: Junior Front End Developer

Web API Lab. The next two deliverables you shall write yourself.

MOdern Java(Script) Server Stack

Some things to watch out for when using PHP and Javascript when building websites

Developing ASP.NET MVC 5 Web Applications

Web Programming and Design. MPT Junior Cycle Tutor: Tamara Demonstrators: Aaron, Marion, Hugh

From the Beginning: Your First Node.js Web Service

Contents in Detail. Foreword by Xavier Noria

Brunch Documentation. Release Brunch team

Beginning HTML. The Nuts and Bolts of building Web pages.

Adding content to your Blackboard 9.1 class

Future Web App Technologies

Transcription:

6.148 Backend III: Node.js with Databases

HELLO AND WELCOME!

Your Feels Lecture too fast!

Your Feels Lecture too fast! Too many languages

Your Feels Lecture too fast! Too many languages Code more in class

Your Feels Lecture too fast! Too many languages Code more in class Stop coding so much in class

Your Feels Lecture too fast! Too many languages Code more in class Stop coding so much in class How do I put it all together?

Your Feels Lecture too fast! Too many languages Code more in class Stop coding so much in class How do I put it all together? More workshops / interactive help!

We re here to help office hours hackathon piazza starter kits annotated sample code

A (very) Brief Review Ideas! Design Usability Graphic design

A (very) Brief Review HTTP request: GET xkcd.com client (you) HTTP response: web content (HTML, CSS, JavaScript) server (xkcd)

A (very) Brief Review Client technologies run on the browser HTML, CSS, (client) JS In Node.js, these are your views and your public directory

A (very) Brief Review Server technologies run on the server Node/Express (server JS), Ruby on Rails Model-view-controller/router Handle a request, send back HTML, etc.

A (very) Brief Review Databases store data persistently We ll use MongoDB with Node Don t worry about installation for now; instructions to come

Where we left off MongoDB is a NoSQL database Convenient -- reads/writes JS objects No structure to data: collections can contain (basically) anything No built-in way to express relations between objects

Our Photos App User inputs photo URL and caption; page that shows photos Last week: stored URLs in a fakedb object Today: store URLs in a real database

Enter Mongoose Library/wrapper on top of MongoDB Lots of nice things: Schemas Validators Methods Relations

package.json "dependencies": { "express": "~4.8.6", "body- parser": "~1.6.6", "cookie- parser": "~1.3.2", "morgan": "~1.2.3", "serve- favicon": "~2.0.1", "debug": "~1.0.4", "ejs": "~0.8.5", "mongodb": "*", "mongoose": "*" }

Schema Structure your data! Can t store anything that doesn t conform to schema var photoschema = mongoose.schema({ caption: String, url: String });

Schema Structure your data! Can t store anything that doesn t conform to schema var photoschema = mongoose.schema({ caption: String, url: String });

Schema Structure your data! Can t store anything that doesn t conform to schema var photoschema = mongoose.schema({ caption: String, url: String });

Models Think of a schema as a declaration for what your data will look like Models are the actual classes that are created in Javascript

Models var photoschema = mongoose.schema({ caption: String, url: String }); var Photo = mongoose.model('photo', photoschema);

Validators Perform additional checks on the data before storing var checklength = function(s) { return s.length > 0; }; Photo.schema.path('caption').validate( checklength, "Caption cannot be empty"); Photo.schema.path('url').validate( checklength, "URL cannot be empty");

Making a DB Query models.photo.findone( {_id: photoid}, function(err, result) { console.log(result); res.render('photo', { photo: result }); });

Making a DB Query models.photo.findone( {_id: photoid}, function(err, result) { console.log(result); res.render('photo', { photo: result }); }); an import from a models file

Making a DB Query models.photo.findone( {_id: photoid}, function(err, result) { console.log(result); res.render('photo', { photo: result }); }); a MongoDB query a query selector

Making a DB Query models.photo.findone( {_id: photoid}, function(err, result) { console.log(result); res.render('photo', { photo: result }); }); a MongoDB query a query callback

Why Callback? DB queries are expensive! If do nothing and wait, our server will be very slow! Send off DB query, do other stuff, when DB query returns then execute the callback

MongoDB Callbacks Always of the form: function(error, result) {...} Result object generally intuitive (what you were looking for, what you just saved, etc.)

MongoDB Callbacks Always of the form: function(error, result) {...} Result object generally intuitive (what you were looking for, what you just saved, etc.) Remember Neander Lin? Check your errors!

Creating New Objects var newphoto = new models.photo({ caption: req.body['submitted- url'], url: req.body['caption'] }); a Mongoose model, imported from models file a simple JS object; note that it satisfies the schema

Not Done Yet... var newphoto = new models.photo({ caption: req.body['submitted- url'], url: req.body['caption'] }); // at this point newphoto is only in memory newphoto.save(function(err, result) { res.redirect('/photos/' + result._id); }); callback looks the same as the find(...) call result is what we just saved

Mongoose does a lot more Helper functions/instance functions References to other objects Not going to demo right now, but will be in annotated code DEMO TIME