CMPE 280 Web UI Design and Development October 19 Class Meeting

Slides:



Advertisements
Similar presentations
MY NCBI (module 4.5). MODULE 4.5 PubMed/How to Use MY NCBI Instructions - This part of the: course is a PowerPoint demonstration intended to introduce.
Advertisements

Presenter: James Huang Date: Sept. 29,  HTTP and WWW  Bottle Web Framework  Request Routing  Sending Static Files  Handling HTML  HTTP Errors.
PHP syntax basics. Personal Home Page This is a Hypertext processor It works on the server side It demands a Web-server to be installed.
PHP and MySQL Database. Connecting to MySQL Note: you need to make sure that you have MySQL software properly installed on your computer before you attempt.
1.  Understanding about How to Working with Server Side Scripting using PHP Framework (CodeIgniter) 2.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
CS 160: Software Engineering August 27 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation MongoDB Write Lecturer.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
CS 235: User Interface Design October 15 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
CS 174: Web Programming September 30 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 160: Software Engineering October 6 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 174: Web Programming August 31 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 174: Web Programming October 14 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 160 and CMPE/SE 131 Software Engineering February 11 Class Meeting Department of Computer Science Department of Computer Engineering San José State.
CMPE 226 Database Systems April 19 Class Meeting Department of Computer Engineering San Jose State University Spring 2016 Instructor: Ron Mak
1 Adding a Model. We have created an MVC web app project Added a controller class. Added a view class. Next we will add some classes for managing movies.
Chapter 1 Getting Started with ASP.NET Objectives Why ASP? To get familiar with our IDE (Integrated Development Environment ), Visual Studio. Understand.
CMPE 280 Web UI Design and Development August 29 Class Meeting
ASP.NET Programming with C# and SQL Server First Edition
(PubMed) MY NCBI (Advanced Course: Module 2)
Introduction to MEAN (JavaScript Full Stack Development)
NodeJS and MEAN cs6320.
NodeJS.
Introduction to Dynamic Web Programming
**Please view the instructional guide as a slideshow**
NodeJS and MEAN Prof. L. Grewe.
CMPE 280 Web UI Design and Development October 26 Class Meeting
CMPE 280 Web UI Design and Development October 17 Class Meeting
3 Things Everyone Knows About Node JS That You Don't
CMPE 280 Web UI Design and Development October 24 Class Meeting
CMPE 280 Web UI Design and Development October 26 Class Meeting
CMPE 280 Web UI Design and Development November 7 Class Meeting
PHP / MySQL Introduction
Build Better Apps with MEAN.
CMPE 280 Web UI Design and Development January 30 Class Meeting
Network Programming Lecture 4: Message Posting
(PubMed) MY NCBI (Advanced Course: Module 2)
ISC440: Web Programming 2 Server-side Scripting PHP 3
CS 174: Server-Side Web Programming January 29 Class Meeting
NodeJS coding basics L. Grewe.
CMPE 152: Compiler Design October 2 Class Meeting
CMPE 280 Web UI Design and Development January 30 Class Meeting
CMPE 152: Compiler Design September 11/13 Lab
(PubMed) MY NCBI (Advanced Course: Module 2)
Abel Sanchez, John R. Williams
CMPE 280 Web UI Design and Development October 18 Class Meeting
CMPE 152: Compiler Design September 27 Class Meeting
CMPE 152: Compiler Design August 28/30 Lab
CMPE 280 Web UI Design and Development January 29 Class Meeting
Web Programming Language
Lecture 5: Functions and Parameters
Tutorial 6 PHP & MySQL Li Xu
CMPE 280 Web UI Design and Development March 19 Class Meeting
Chengyu Sun California State University, Los Angeles
CMPE 152: Compiler Design February 21/26 Lab
CMPE 152: Compiler Design February 28 / March 5 Lab
Wells Fargo Toolkit – CreativeBuilder Reference Guide
Chengyu Sun California State University, Los Angeles
Building Apps in Azure with only Scripting Skills
CMPE 152: Compiler Design April 18 – 30 Labs
Chengyu Sun California State University, Los Angeles
CMPE 152: Compiler Design March 7/12 Lab
MEAN login management CS252.
CMPE 280 Web UI Design and Development August 27 Class Meeting
CMPE 152: Compiler Design September 17 Class Meeting
CMPE 280 Web UI Design and Development March 19 Class Meeting
CMPE 280 Web UI Design and Development September 10 Class Meeting
Presentation transcript:

CMPE 280 Web UI Design and Development October 19 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor: Ron Mak www.cs.sjsu.edu/~mak

Express/MongoDB Example A very simple example of Express on the server side accessing a MongoDB database. We will display some user documents that each contains a user name and an email address. Recall that a document is analogous to a record in a relational database. Recall that a collection is analogous to to a relational table. We will add new documents.

Install the Express Generator The generator creates project scaffolding. ~: npm install -g express-generator (node:24252) fs: re-evaluating native module sources is not supported. If you are using the graceful-fs module, please update it to a more recent version. /usr/local/bin/express -> /usr/local/lib/node_modules/express-generator/bin/express-cli.js /usr/local/lib └── express-generator@4.15.5 

Create a New Express Project ~: express nodetest1   warning: the default view engine will not be jade in future releases   warning: use `--view=jade' or `--help' for additional options    create : nodetest1    create : nodetest1/package.json    create : nodetest1/app.js    create : nodetest1/public    create : nodetest1/routes    create : nodetest1/routes/index.js    create : nodetest1/routes/users.js    create : nodetest1/views    create : nodetest1/views/index.jade    create : nodetest1/views/layout.jade    create : nodetest1/views/error.jade    create : nodetest1/bin    create : nodetest1/bin/www    create : nodetest1/public/javascripts    create : nodetest1/public/images    create : nodetest1/public/stylesheets    create : nodetest1/public/stylesheets/style.css    install dependencies:      $ cd nodetest1 && npm install    run the app:      $ DEBUG=nodetest1:* npm start

Add Project Dependencies Modify nodetest1/package.json { ...   "dependencies": {     "body-parser": "~1.18.2",     "cookie-parser": "~1.4.3",     "debug": "~2.6.9",     "express": "~4.15.5",     "jade": "~1.11.0",     "mongodb": "^2.2.25",     "monk": "^4.0.0",     "morgan": "~1.9.0",     "serve-favicon": "~2.4.5"   } } Monk: “A tiny layer that provides simple yet substantial usability improvements for MongoDB usage within Node.JS.” https://www.npmjs.com/package/monk

Start the node.js Web Server First install the dependencies: Then start the node.js web server: Send your web browser to localhost:3000 ~: npm install ~: npm start > nodetest1@0.0.0 start /Users/rmak/nodetest1 > node ./bin/www Demo

“Hello World” In routes/index.js var express = require('express'); var router = express.Router(); /* GET home page. */ router.get('/', function(req, res, next) {   res.render('index', { title: 'Express' }); }); /* GET Hello World page. */ router.get('/helloworld', function(req, res) {     res.render('helloworld', { title: 'Hello, World!' });

“Hello World” cont’d In views/helloworld.jade Send your web browser to localhost:3000/helloworld extends layout block content   h1= title   p Hello, World! Welcome to #{title} Demo

Initialize the MongoDB Database Start the MongoDB shell. Insert the first document: /usr/local/Cellar/mongodb/3.2.10: mongo MongoDB shell version: 3.2.10 connecting to: test Server has startup warnings:  2017-10-19T02:30:02.649-0700 I CONTROL  [initandlisten]  2017-10-19T02:30:02.649-0700 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000 > use nodetest1 switched to db nodetest1 > db.usercollection.insert({ "username" : "testuser1", "email" : "testuser1@testdomain.com" }) WriteResult({ "nInserted" : 1 }) Collection usercollection is automatically created the first time we add to it.

Initialize the MongoDB Database, cont’d > db.usercollection.find().pretty() { "_id" : ObjectId("59e8711733b5f794be534cda"), "username" : "testuser1", "email" : "testuser1@testdomain.com" } Yes, it was successfully inserted. So insert two more documents.

Initialize the MongoDB Database, cont’d > newstuff = [{ "username" : "testuser2", "email" : "testuser2@testdomain.com" }, { "username" : "testuser3", "email" : "testuser3@testdomain.com" }] [ { "username" : "testuser2", "email" : "testuser2@testdomain.com" }, "username" : "testuser3", "email" : "testuser3@testdomain.com" } ] > db.usercollection.insert(newstuff); BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 2, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] })

Initialize the MongoDB Database, cont’d > db.usercollection.find().pretty() { "_id" : ObjectId("59e8711733b5f794be534cda"), "username" : "testuser1", "email" : "testuser1@testdomain.com" } "_id" : ObjectId("59e8715233b5f794be534cdb"), "username" : "testuser2", "email" : "testuser2@testdomain.com" "_id" : ObjectId("59e8715233b5f794be534cdc"), "username" : "testuser3", "email" : "testuser3@testdomain.com"

Connect to the MongoDB Database In app.js Use Monk to connect to the database nodetest1 at the default MongoDB port 27017. Make the database accessible to the router: var mongo = require('mongodb'); var monk = require('monk'); var db = monk('localhost:27017/nodetest1'); app.use(function(req, res, next) { req.db = db; next(); } );

Display Database Data In routes/index.js /* GET Userlist page. */ router.get('/userlist', function(req, res) {     var db = req.db;     var collection = db.get('usercollection');     collection.find({}, {}, function(e, docs)         res.render('userlist', {             "userlist" : docs         });     }); } ); Fill the docs with database documents and then render them.

Display Database Data, cont’d In views/userlist.jade Send your web browser to localhost:3000/userlist block content     h1.         User List     ul         each user, i in userlist             li                 a(href="mailto:#{user.email}")= user.username Demo

Display the New User Form In routes/index.js /* GET New User page. */ router.get('/newuser', function(req, res) {     res.render('newuser', { title: 'Add New User' }); });

Display the New User Form, cont’d In views/newuser.jade Send your web browser to localhost:3000/newuser extends layout block content     h1= title     form#formAddUser(name="adduser",method="post",action="/adduser")         input#inputUserName(type="text", placeholder="username", name="username")         input#inputUserEmail(type="text", placeholder="useremail", name="useremail")         button#btnSubmit(type="submit") submit Demo

Add a New User In routes/index.js /* POST to Add User Service */ router.post('/adduser', function(req, res) {     // Set our internal DB variable     var db = req.db;     // Get our form values. These rely on the "name" attributes     var userName = req.body.username;     var userEmail = req.body.useremail;     // Set our collection     var collection = db.get('usercollection');

Add a New User, cont’d In routes/index.js, cont’d // Submit to the DB     collection.insert( { "username" : userName,         "email" : userEmail }, function (err, doc) {         if (err)             // If it failed, return error             res.send("There was a problem.");         }         else             // And forward to success page             res.redirect("userlist");     }); });

Add a New User, cont’d Send your web browser to localhost:3000/newuser The completed application would have much more error checking. Demo

Assignment #4 Add a MongoDB database to your project. Create web pages to: display documents add and delete documents update documents You must use Express and either Monk or Mongoose to talk to the database. Monk: https://www.npmjs.com/package/monk Mongoose: https://www.npmjs.com/package/express-mongoose