Building spatial back ends with Node.js and MongoDB
-
Upload
shekhar-gulati -
Category
Technology
-
view
3.293 -
download
2
description
Transcript of Building spatial back ends with Node.js and MongoDB
![Page 1: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/1.jpg)
OPENSHIFTWorkshop
PRESENTEDBY
ShekharGulati
Building Spatial Back-ends with Node.js and MongoDB
![Page 2: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/2.jpg)
AGENDA
➔ Learn a bit about Node.js and Express
➔ Learn some MongoDB basics
➔ Build a location aware Job search application
➔ Go live with a Node.js MongoDB powered app in ~ 45 mins
![Page 3: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/3.jpg)
Write an application and host it on internet
http://localjobsdemo-shekhargulati.rhcloud.com/
GOAL
![Page 4: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/4.jpg)
CODE DU JOUR
https://github.com/shekhargulati/localjobs-nodejs
![Page 5: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/5.jpg)
● You know JavaScript● You know Git● You like to write code rather than
managing servers.
ASSUMPTIONS
![Page 6: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/6.jpg)
There are no stupid questions only stupid answers. So ask questions
ASK QUESTIONS
![Page 7: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/7.jpg)
Node.js provides a purely event-driven , non-blocking infrastructure to script
highly concurrent programs.
NODE.JS
![Page 8: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/8.jpg)
NODE.JS EXPLAINED
![Page 9: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/9.jpg)
Node.js goal is to provide an easy way to build scalable network applications.
Some ex. Web servers , TCP servers, Streaming server , Realtime apps, etc.
NODE.JS GOAL
![Page 10: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/10.jpg)
Node.js is Server Side JavaScript.
● It provides runtime environment● It provides a library
NODE.JS IS
![Page 11: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/11.jpg)
● Non Blocking I/O● Based on Google's Chrome V8 engine● 40k + modules● Active community● Multi platform – Windows , Mac , and Linux● Use single language for both client and
server.● JavaScript is language of Web.
WHY NODE.JS
![Page 12: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/12.jpg)
● Go to http://nodejs.org/ and click install.● Install the downloaded package.
INSTALLING NODE.JS
![Page 13: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/13.jpg)
● Hello World● HTTP Server
NODE.JS IN ACTION
![Page 14: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/14.jpg)
● Routing support● Template support● Error handling● Etc.
![Page 15: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/15.jpg)
EXPRESS TO RESCUE
Web Application Framework for Node
![Page 16: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/16.jpg)
● Install Express– npm install express -g
● Create a new Express application– express --sessions --ejs hello-world
![Page 17: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/17.jpg)
MONGODB
![Page 18: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/18.jpg)
18
Open Source NoSQL document datastore– JSON style documents
Schema-less– Each document is heterogeneous, and may have completely
unique structure compared to other documents
Fast and horizontally scalable Rich query language Rich documents Easy to get running Geospatial indexing
WHAT IS MONGODB
![Page 19: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/19.jpg)
Database → Database
Table → Collection
Row → Document
Index → Index
STEP 4 : MONGODB TERMINOLOGY
![Page 20: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/20.jpg)
DEMO TIME
![Page 21: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/21.jpg)
21
What is it for? Find all the MongoDB jobs near me – Proximity Queries Find all the MongoDB jobs within Bangalore – Bounded
Queries Find all the MongoDB job at this location – Exact Queries
● Supports only two dimensional indexes. You can only have one geospatial index per collection. By default, 2d geospatial indexes assume longitude
and latitude have boundaries of -180 inclusive and 180 non-inclusive (i.e. [-180, 180))
GEOSPATIAL INDEXING BASICS
![Page 22: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/22.jpg)
22
1) Put your coordinates into an array
{ loc : [ 50 , 30 ] } //SUGGESTED OPTION
{ loc : { x : 50 , y : 30 } }
{ loc : { foo : 50 , y : 30 } }
1) { loc : { lon : 40.739037, lat: 73.992964 } }
2) Make a 2d index
db.places.ensureIndex( { loc : "2d" } )
3) If you use latitude and longitude as your coordinate system, always store longitude first. MongoDB’s 2d spherical index operators only recognize [ longitude, latitude] ordering.
HOW TO MAKE IT WORK
![Page 23: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/23.jpg)
23
APP DEVELOPMENT
![Page 24: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/24.jpg)
LocalJobs : Location Aware Job Search App
Functionalities or User stories or Features
1. As an Employer , I should be able to register my company
2. As an Employer, I should be able to post jobs
3. As a Job seeker , I should be able to register myself with application
4. As a Job seeker , I should be able to search jobs near to my current location.
5. As a Job seeker , I should be able to apply a job
6. Etc...
APP -- USER STORIES
![Page 25: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/25.jpg)
Prioritize the functionalities or user stories or features
– As an Employer, I should be able to post jobs
– As a Job seeker , I should be able to search jobs near to my current location.
– As a Job seeker , I should be able to see all the jobs
PRIORITIZE
![Page 26: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/26.jpg)
$ git clone https://github.com/shekhargulati/localjobs-nodejs.git
STEP 0 : CLONE REPO
![Page 27: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/27.jpg)
$ git checkout 380e795c9b52235838c361f9052f188e054c0ea8
$ npm install -l
STEP 1 : APP INIT
![Page 28: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/28.jpg)
$ git checkout b721308a8945b28019731791a5a1188ba3d855b6
$ npm install -l
STEP 2 : INDEX PAGE
![Page 29: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/29.jpg)
$ git checkout edbec3f6df758e6287f7c8ded6098ada9262282
$ npm install -l
STEP 3 : SAVE JOB
![Page 30: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/30.jpg)
$ git checkout 457eb572b0d74c649b56c26306a1d5bcdd01a9e1
STEP 4 : VIEW ALL JOBS
![Page 31: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/31.jpg)
$ git checkout a9e50f4feb1448c924758ed968d8253da3c06070
Create MongoDB 2d Index
STEP 5 : SEARCH BY NEAR
![Page 32: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/32.jpg)
$ git checkout 82a6cefe0514d1b81d0b8446412a16340465590d
STEP 6 : SEARCH BY GEONEAR
![Page 33: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/33.jpg)
$ ./mongoimport --db localjobs --collection jobs –file jobs.json
STEP 7 : IMPORT DATA
![Page 34: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/34.jpg)
34
APPLICATION DEPLOYMENT
![Page 35: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/35.jpg)
![Page 36: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/36.jpg)
![Page 37: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/37.jpg)
PaaS == Platform as a Service
A Cloud Application Platform
Code Deploy Enjoy
Save Time and MoneyCode your app
Push-button Deploy, and your App is running in the Cloud!
![Page 38: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/38.jpg)
38
OpenShiftis
PaaS by Red Hat
Multi-language,Auto-Scaling,Self-service,
Elastic,Cloud Application
Platform
![Page 39: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/39.jpg)
39
OUR STACK
![Page 40: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/40.jpg)
40
https://openshift.redhat.com/app/account/newPromo code is JSCHANNEL13
CREATING OPENSHIFT ACCOUNT
![Page 41: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/41.jpg)
● Free! No time limit
● 3 gears (like servers) - each 512 Mb RAM, 1 Gb disk
● Auto-scaling
● Simple pricing
BUT WAIT – THERE's MORE
![Page 42: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/42.jpg)
42
rhc create-app localjobs nodejs mongodb-2.2 --from-code=https://github.com/shekhargulati/localjobs-nodejs.git
Lot of other tasks you can do with rhc – tail log, app management, cartridge management , ssh management , namespace management, etc. Run rhc -h for details
DEMO : LOCALJOBS APP
RHC is only required for infrastructure tasks
![Page 43: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/43.jpg)
43
git push
![Page 44: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/44.jpg)
IN CASE YOU CARE
• Shekhar Gulati – Works at Red Hat
• Principal OpenShift Developer Evangelist
• Java / Python/ JavaScript / NoSQL / Cloud Guy
• Twitter Handle : shekhargulati
• Github https://github.com/shekhargulati
• Slides https://speakerdeck.com/shekhargulati
![Page 45: Building spatial back ends with Node.js and MongoDB](https://reader034.fdocuments.in/reader034/viewer/2022042714/554f9298b4c905d25b8b52a7/html5/thumbnails/45.jpg)
QUESTIONS?