MEAN JS에서 응용 프로그램을 개발 중입니다. MEAN JS를 처음 사용합니다.
다음과 같이 json respone을 얻기 위해 외부 API에 액세스하고 싶습니다. 여기
{"id":"7gd6ud7ud5r0c","name":"jack","zip":"94109","gender":"Male"}
에이 참조가 있습니다 (https://nodejs.org/api/https.html) ..
하지만 내부에서 http / https 요청을 사용하는 방법을 모르겠습니다. 클라이언트 컨트롤러.
여기 내 express.js가 있습니다.
'use strict';
/**
* Module dependencies.
*/
var fs = require('fs'),
http = require('http'), // required already
https = require('https'), // required already
express = require('express'),
morgan = require('morgan'),
bodyParser = require('body-parser'),
.....
.....
.....
여기 내 invite.client.controller.js가 있습니다.
'use strict';
angular.module('core').controller('InviteController', ['$scope',
'Authentication', '$http',
function($scope, Authentication, $http) {
// This provides Authentication context.
$scope.authentication = Authentication;
$scope.getMembersFromAPI = function(){
***************************************
// this block shows error ReferenceError: require is not defined
var http = require('http'),
https = require('https');
***************************************
var options = {
hostname: 'https://api.somedemodp.com/v5/td?api_key=ec96c9afcbb6bbb8f5a687bd7&[email protected]',
path: '/',
method: 'GET'
};
var req = https.request(options, function(res) {
console.log('statusCode: ', res.statusCode);
console.log('headers: ', res.headers);
res.on('data', function(d) {
process.stdout.write(d);
});
});
req.end();
req.on('error', function(e) {
console.error(e);
});
};
}
]);
... 각진 코드와 표현 코드를 혼동하고 있습니까? 각도에서 require를 사용하지 않습니다. 의존성 주입을 사용합니다.
Angular는 프런트 엔드이고 Express는 백엔드입니다. 그들은 분리되어 있습니다.
angular.module('core').controller('InviteController', ['$scope',
'Authentication', '$http',
function($scope, Authentication, $http)
여기에서 종속성 주입을 수행합니다 (노드 / 익스프레스에서 요구하는 것과 유사). 이미 $ http를 주입했습니다.
실제로 문서에서 $ http 호출을 사용하여 angular 내에서 직접 외부 API를 호출 할 수 있습니다.
// Simple GET request example :
$http.get('/someUrl').
success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다