简体   繁体   中英

How to obtain data from RESTful api in nodejs with middleware?

let's say we have an api:

/api/people/id

and we want to get people with id:678, then render a page with this piece of info. I assume this can be done something similar to this:

router.get('/', function(req, res, next) {
  var people = get("/api/people/678");
  res.render('people-profile', people);
});

and after some research i found that http.request may be capable of doing this kind of task, but it seems way too complex! and i start wondering what is an api designed for. Am I using api in a wrong way? Is api designed to be used out of server side, is it designed to be used in ajax?

A REST Server simply provides access to resources and REST client accesses and modifies the resources using HTTP protocol .

So you could access and modify that data via AJAX Requests or without , but over HTTP protocol. The main point is to understand how to work with data behind REST API - perform CRUD(create, read, update, delete) operations.

I suggest you to get familiar with Node.js RESTful API article , that should give you clear understanding of how to use REST API, if after reading article above you would still not understand of how to use REST API - consider read more articles (just google for "how REST work" and search for articles) or watch video tutorials.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM