SlideServe
Menu
Préférences
© SlideServe · v2026

RESTful Web Development With Nodejs and Express

lave avatar
7 diapositives · 489 Views · Jul 29, 2014

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

RESTful Web Development With Nodejs and Express - slide 1
01 / 7RESTful Web Development With Nodejs and Express
1 / 17

Télécharger la présentation

RESTful Web Development With Nodejs and Express
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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();