1 / 7

RESTful Web Development With Nodejs and Express

RESTful Web Development With Nodejs and Express. REST. Stands for REpresentational State Transfer Has the following constraints: Client-Server Stateless Cacheable Layered System Code on Demand Uniform Interface. REST on the web. Every kind of resource you want is at a different URI

lave
Download Presentation

RESTful Web Development With Nodejs and Express

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. RESTful Web Development With Nodejs and Express

  2. REST • Stands for REpresentational State Transfer • Has the following constraints: • Client-Server • Stateless • Cacheable • Layered System • Code on Demand • Uniform Interface

  3. REST on the web • Every kind of resource you want is at a different URI • Interact with resources using the HTTP verbs • PUT (Create) • GET (Read) • POST (Update) • DELETE (Delete) • The server does not keep track of state (except for authentication) • The client manages the logic of the application.

  4. REST Examples • Assume we are making an app to track what people we meet with • We need the following resources: • Users • Meetings • We might use the following URIs • GET /user (list all users) • POST/user/1 (get information about user with ID 1) • PUT /meeting (create a new meeting) • DELETE /meeting /5 (delete meeting with ID 5) • The responses are usually JSON, e.g. { 'id': 1, 'name': 'John MacDonald', 'hometown': 'Halifax', 'occupation': 'Farmer' }

  5. Using NodeJS • NodeJS is just a JavaScript interpreter. • It comes with a package manager called npm • Install packages like this: npm install <package_name> • This will install it in the current folder. • To install globally, do npm install –g <package_name> • To use Node as a webserver, you must write an application that responds to web requests. • Node has a library (HTTP) for doing this, but it’s easier to use a framework, like Express • To access a library, use the require() function

  6. Using Express • Express is just a package for Node • Create a new web application with var app = express(); • Respond to requests like app.get('/user', function(req, res){ • Look at parameters through the req object • req.params for query parameters • req.body for post fields • req.files for files • Send responses through the res object • res.send("Hi mom!") • Start the application with • app.listen(<port>)

  7. Client-Server Communication • On the client side, use XMLHttpRequest (or jQuery) • If you are not getting your data from the same place as your page, then you will have to use Cross Origin Resource Sharing (CORS) varxhr=newXMLHttpRequest(); //Might also use onreadystatechange xhr.onload=function() { varresponseObject = JSON.parse(this.responseText); }; xhr.open('get', 'user'); xhr.send();

More Related