服务器端获取请求

server side get request

本文关键字:请求 获取 服务器端      更新时间:2023-09-26

我正在请求Coursera api在客户端,但由于CORS它被阻止。和json也不工作,因为api返回json数据。

我大部分时间都在客户端工作,所以不太了解服务器端请求。

我如何获得api数据使用节点,然后使用该数据来创建网页。我在前端使用Angular。

我已经搜索了如何在节点GET请求,我也得到了一些答案(使用http模块)。但就是不知道前端怎么连接?

require('http')

HTTP模块文档:https://nodejs.org/api/http.html

用法:

    http.get({
  hostname: 'localhost',
  port: 80,
  path: '/',
  agent: false  // create a new agent just for this one request
}, (res) => {
  // Do stuff with response
});

获取API代码(未测试)

http.get({
    hostname: 'https://api.coursera.org',
    port: 80,
    path: '/api/courses.v1',
    agent: false
}, (res) => {
    res // your json data
});