앵귤러JS(AngularJS) 비동기 파일 업로드 (ng-file-upload)
http://www.cchan.me/?p=64
bower install ng-file-upload --save
<form ng-app="fileUpload" ng-controller="MyCtrl" name="form">
<div class="button" ng-model="file" name="file" ngf-select ngf-pattern="'image/*'" ngf-accept="'image/*'" ngf-max-size="20MB">파일선택</div>
<button type="submit" ng-click="submit()">업로드</button>
</form>
var app = angular.module('fileUpload', ['ngFileUpload']);
app.controller('MyCtrl', ['$scope', 'Upload', function ($scope, Upload) {
$scope.submit = function() {
if ($scope.form.file.$valid && $scope.file) {
$scope.upload($scope.file);
}
};
$scope.upload = function (file) {
Upload.upload({
url: 'upload/url',
data: {file: file, 'username': $scope.username}
}).then(function (resp) {
console.log('Success ' + resp.config.data.file.name + 'uploaded. Response: ' + resp.data);
}, function (resp) {
console.log('Error status: ' + resp.status);
}, function (evt) {
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
});
};
}]);
아래 레퍼런스는 생각보다 도움이 안되었다.
http://webframeworks.kr/tutorials/angularjs/angularjs_fileupload/
Q promise로 차례대로 비동기작업 실행하기 (Node.js)
https://blog.outsider.ne.kr/1126
var Q = require('q');
var list = [
{id: 1, name: 'name1'},
{id: 2, name: 'name2'},
{id: 3, name: 'name3'},
{id: 4, name: 'name4'},
{id: 5, name: 'name5'}
];
var asyncJob = function(v, a) {
var deferred = Q.defer();
setTimeout(function() {
console.log(v);
deferred.resolve(1);
}, 1000);
return deferred.promise;
};
function next(idx) {
if (idx > list.length -1) { return Q(true); }
return asyncJob(list[idx]).then(function(result) {
return next(idx+ 1);
});
}
next(0).then(function() {
console.log('completed');
});
JWT 토큰 인증 구현
http://opens.kr/81
1) npm 설치
$ npm install jsonwebtoken
jsonwebtoken@5.0.2 node_modules/jsonwebtoken
└── jws@3.0.0 (jwa@1.0.0, base64url@1.0.4)
2) jwt 토큰 생성 및 실행
var jwt = require('jsonwebtoken');
var tokenKey = "TEST_KEY11"; //토큰키 서버에서 보관 중요
var payLoad = {'uid':14554};
var token = jwt.sign(payLoad,tokenKey,{
algorithm : 'HS256', //"HS256", "HS384", "HS512", "RS256", "RS384", "RS512" default SHA256
expiresInMinutes : 1440 //expires in 24 hours
});
console.log("token : ", token);
$ node jwt_test.js
token : eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjE0NTU0LCJpYXQiOjE0MzUxMzA4NzMsImV4cCI6MTQzNTIxNzI3M30.EWNUjnktCWxlqAAZW2bb0KCj5ftVjpDBocgv2OiypqM
2) jwt 토큰 디코딩
var jwt = require('jsonwebtoken');
var tokenKey = "TEST_KEY11"; //토큰키 서버에서 보관 중요
var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjE0NTU0LCJpYXQiOjE0MzUxMzA4NzMsImV4cCI6MTQzNTIxNzI3M30.EWNUjnktCWxlqAAZW2bb0KCj5ftVjpDBocgv2OiypqM';
//비동기처리
jwt.verify(token,tokenKey,function(err,decoded){
console.log("sync : ", decoded);
});
//동기처리
try {
var decoded = jwt.verify(token,tokenKey);
console.log("async : ", decoded);
} catch(err){
console.log(err);
}
$ node jwt_test_decoded.js
async : { uid: 14554, iat: 1435130873, exp: 1435217273 }
sync : { uid: 14554, iat: 1435130873, exp: 1435217273 }
//실행은 sync가 먼저 되었지만 비동기 방식이므로 더 뒤에 출력됨
Node.js 문자열 생성 시 길이 제한
https://stackoverflow.com/questions/24153996/is-there-a-limit-on-the-size-of-a-string-in-json-with-node-js
JSON.stringify() 메소드를 이용하여 객체를 문자열로 직접 바꾸는 것은 분명히 크기 제한이 있는 것 같다.
큰 문자열을 생성해야 하는 경우에는 객체를 통으로 문자열로 변환하기보다는 쪼개서 문자열 연산을 하도록 하는 것이 좋을 듯
| The maximum string size in "vanilla" nodeJS (v0.10.28) is in the ballpark of 1GB. If your are in a hurry, you can test the maximum supported string size with a self doubling string. The system tested has 8GB of RAM, mostly unused. x = 'x';
while (1){
x = ''+x+x; // string context
console.log(x.length);
}
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
FATAL ERROR: JS Allocation failed - process out of memory
Aborted (core dumped)
In another test I got to 1,000,000,000 with a one char at a time for loop. Now a critic might say, "wait, what about JSON. the question is about JSON!" and I would shout THERE ARE NO JSON OBJECTS IN JAVASCRIPT the JS types are Object, Array, String, Number, etc.... and as JSON is a String representation this question boils down to what is the longest allowed string. But just to double check, let's add a JSON.stringify call to address the JSON conversion. Code x = 'x';
while (1){
x = ''+x+x; // string context
console.log(JSON.stringify({a:x}).length);
}
Expectations: the size of the JSON string will start greater than 2, because the first object is going to stringify to '{"a":"xx"}' for 10 chars. It won't start to double until the x string in property a gets bigger. It will probably fail around 256M since it probably makes a second copy in stringification. Recall a stringification is independent of the original object. Result: 10
12
16
24
40
72
136
264
520
1032
2056
4104
8200
16392
32776
65544
131080
262152
524296
1048584
2097160
4194312
8388616
16777224
33554440
67108872
134217736
268435464
Pretty much as expected.... Now these limits are probably related to the C/C++ code that implements JS in the nodeJS project, which at this time I believe is the same V8 code used in Chrome browsers. There is evidence from blog posts of people recompiling nodeJS to get around memory limits in older versions. There are also a number of nodejs command line switches. I have not tested the effect of any of this. |