Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Creating a RESTful Web Service
●I'm Randal Kamradt – Building REST services for 10 years.
●All code for this presentation,...
Creating a RESTful Web Service
Prerequisites:
1. Git command line
2. Node.js
3. Software for compiling native Node modules...
Check all prerequisites
What is RESTful
Representational State Transfer.
Uses HTTP verbs GET, PUT, POST, DELETE.
Uses common HTTP status codes.
Ve...
Kill server (ctrl-c) and save project
Create A GitHub Repo
Fill in basic details
Copy URL to clipboard
Clone repo to local drive
Create NPM Project
Install Dependencies
● Runtime dependencies:
npm install –save
● express
● body-parser
● method-override
● mongodb
● morgan
Install Dependencies
● Development dependencies:
npm install –save-dev
● grunt
● grunt-contrib-clean
● grunt-contrib-copy
...
Check package.json
Browse to my Gists
https://gist.github.com/rkamradt
Using a text editor or IDE copy the
gists into your project
● Gruntfile.js: basic express grunt file
● index.js: server.js...
Run server and browse to it
Kill server (ctrl-c) and save project
Go back and check GitHub
Create a dataloader for Mongo
● Copy the mongoload.js gist into
server/mongoload.js
● Create a directory called test and a...
Fill in test-data.js with an array of
strings
{ "data":
[
{ "name": "mercury" },
{ "name": "venus" },
{ "name": "earth" },...
Add mongoloader to server.js:
var mongoload = require('./server/mongoload');
mongoload.load(function(err) {
if(err) {
cons...
Add Mongo get to router.js
var mongoload = require('./monogload');
...
if(req.method === 'GET') {
mongoload.getAll(functio...
Run server again and browse to it
Making a RESTful Web Sevice
(the easy way)
● Change directory to backout of your project
(cd ..)
● Make a new directory an...
Yoeman Boilerplate
Upcoming SlideShare
Loading in …5
×

Meetup-js-032815

452 views

Published on

Randy Kamradt Sr - Creating web-services using Express.js and MongoDB.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Meetup-js-032815

  1. 1. Creating a RESTful Web Service ●I'm Randal Kamradt – Building REST services for 10 years. ●All code for this presentation, including these slides is on GitHub at https://github.com/rkamradt/my-first-rest ●On twitter at @rkamradt ●Blogging about programming at https://rlkamradt.wordpress.com/
  2. 2. Creating a RESTful Web Service Prerequisites: 1. Git command line 2. Node.js 3. Software for compiling native Node modules 4. MongoDB 5. Grunt command line 6. A GitHub account 7. Access to the command line 8. A Text editor or IDE
  3. 3. Check all prerequisites
  4. 4. What is RESTful Representational State Transfer. Uses HTTP verbs GET, PUT, POST, DELETE. Uses common HTTP status codes. Very simple to implement with an HTTP server. Very simple to access via the Internet. Not a standard. Consensus among implementers. Many common front-end frameworks expect it. For more information go to : http://en.wikipedia.org/wiki/Representational_state_transfer
  5. 5. Kill server (ctrl-c) and save project
  6. 6. Create A GitHub Repo
  7. 7. Fill in basic details
  8. 8. Copy URL to clipboard
  9. 9. Clone repo to local drive
  10. 10. Create NPM Project
  11. 11. Install Dependencies ● Runtime dependencies: npm install –save ● express ● body-parser ● method-override ● mongodb ● morgan
  12. 12. Install Dependencies ● Development dependencies: npm install –save-dev ● grunt ● grunt-contrib-clean ● grunt-contrib-copy ● grunt-contrib-jshint ● grunt-contrib-watch ● grunt-express-server ● grunt-simple-mocha ● jshint ● load-grunt-tasks ● mocha ● should
  13. 13. Check package.json
  14. 14. Browse to my Gists https://gist.github.com/rkamradt
  15. 15. Using a text editor or IDE copy the gists into your project ● Gruntfile.js: basic express grunt file ● index.js: server.js ● server/router.js: Skeleton Express Route
  16. 16. Run server and browse to it
  17. 17. Kill server (ctrl-c) and save project
  18. 18. Go back and check GitHub
  19. 19. Create a dataloader for Mongo ● Copy the mongoload.js gist into server/mongoload.js ● Create a directory called test and a file in it called test-data.js
  20. 20. Fill in test-data.js with an array of strings { "data": [ { "name": "mercury" }, { "name": "venus" }, { "name": "earth" }, { "name": "mercury" }, { "name": "mars" }, { "name": "jupiter" }, { "name": "saturn" }, { "name": "neptune" }, { "name": "uranus" } ] }
  21. 21. Add mongoloader to server.js: var mongoload = require('./server/mongoload'); mongoload.load(function(err) { if(err) { console.log('error loading data into mongo'); return; } http.createServer(app).listen(9999, function() { console.log('Server up: http://localhost:' + 9999); }); });
  22. 22. Add Mongo get to router.js var mongoload = require('./monogload'); ... if(req.method === 'GET') { mongoload.getAll(function(err, result) { if(err) { res.status(500).send(err.message).end(); } else { res.status(200).send(JSON.stringify(result)).end(); } }); return; // dont end until after callback } else...
  23. 23. Run server again and browse to it
  24. 24. Making a RESTful Web Sevice (the easy way) ● Change directory to backout of your project (cd ..) ● Make a new directory and change to it ● Run sudo npm install -g yo ● Run sudo npm install -g generator-meanjs ● Run yo meanjs ● Run grunt ● Open browser to http://localhost:3000
  25. 25. Yoeman Boilerplate

×