Programming/node.js2019. 5. 15. 13:38

흐음.. 캐싱할때 이걸 어떻게 상태로 캐싱을 해놓으면 되려나..

 

app.use(function forceLiveDomain(req, res, next) {
  // Don't allow user to hit Heroku now that we have a domain
  var host = req.get('Host');
  if (host === 'serviceworker-cookbook.herokuapp.com') {
    return res.redirect(301, 'https://serviceworke.rs/' + req.originalUrl);
  }
  return next();
});

 

[링크 : https://davidwalsh.name/express-redirect-301]

[링크 : https://stackoverflow.com/questions/7450940/automatic-https-connection-redirect-with-node-js-express]

 

res.redirect([status,] path)
Redirects to the URL derived from the specified path, with specified status, a positive integer that corresponds to an HTTP status code . If not specified, status defaults to “302 “Found”.

[링크 : https://expressjs.com/ko/4x/api.html#res.redirect]

'Programming > node.js' 카테고리의 다른 글

npm-run-all 병렬 빌드 (실패)  (0) 2019.05.29
pdf 내용 추출  (0) 2019.05.27
node.js 항목 확인  (0) 2019.04.23
proxy error: Error: write after end  (0) 2019.04.23
node.js 시그널 핸들링과 reload  (0) 2019.04.23
Posted by 구차니
Programming/node.js2019. 4. 23. 18:17

와.. 'undefined' 랑 비교 안해도 되는 저런 멋진 문법?

if('content-length' in ctx.proxyToServerRequestOptions.headers)

[링크 : https://github.com/.../examples/removeProxyToServerContentLength.js]

[링크 : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in]

'Programming > node.js' 카테고리의 다른 글

pdf 내용 추출  (0) 2019.05.27
node.js express 301 redirect  (0) 2019.05.15
proxy error: Error: write after end  (0) 2019.04.23
node.js 시그널 핸들링과 reload  (0) 2019.04.23
nodejs url param delete  (0) 2019.04.17
Posted by 구차니
Programming/node.js2019. 4. 23. 18:07

흐음.. 왜 안써지냐..

기본 예제로는 response.end(data) 식이었고..

얘는 response.write(data, endcoding); response.end(callback); 두개 문장으로 실행되는 유사한 효과를 준다는데

그럼 되는거 아닌가? ㅠㅠ

 

response.end([data][, encoding][, callback])#

History

This method signals to the server that all of the response headers and body have been sent; that server should consider this message complete. The method, response.end(), MUST be called on each response.

If data is specified, it is similar in effect to calling response.write(data, encoding) followed by response.end(callback).

If callback is specified, it will be called when the response stream is finished.

[링크 : https://nodejs.org/api/http.html#http_class_http_serverresponse]

 

 

+

으으 망할.. async 문제인가?

[링크 : https://teamtreehouse.com/community/error-write-after-end-3]

'Programming > node.js' 카테고리의 다른 글

node.js express 301 redirect  (0) 2019.05.15
node.js 항목 확인  (0) 2019.04.23
node.js 시그널 핸들링과 reload  (0) 2019.04.23
nodejs url param delete  (0) 2019.04.17
url 끝의 /  (0) 2019.04.17
Posted by 구차니
Programming/node.js2019. 4. 23. 13:31

process.on 으로 시그널 핸들러를 연결해두는 듯

 

var cluster = require('cluster');

console.log('started master with ' + process.pid);

//fork the first process
cluster.fork();

process.on('SIGHUP', function () {
  console.log('Reloading...');
  var new_worker = cluster.fork();
  new_worker.once('listening', function () {
    //stop all other workers
    for(var id in cluster.workers) {
      if (id === new_worker.id.toString()) continue;
      cluster.workers[id].kill('SIGTERM');
    }
  });
});

[링크 : https://joseoncode.com/2015/01/18/reloading-node-with-no-downtime/]

[링크 : http://zguide.zeromq.org/js:interrupt]

[링크 : https://stackoverflow.com/questions/20165605/detecting-ctrlc-in-node-js]

'Programming > node.js' 카테고리의 다른 글

node.js 항목 확인  (0) 2019.04.23
proxy error: Error: write after end  (0) 2019.04.23
nodejs url param delete  (0) 2019.04.17
url 끝의 /  (0) 2019.04.17
js array key 삭제하기  (0) 2019.04.16
Posted by 구차니
Programming/node.js2019. 4. 17. 22:06

url 파라미트에서 특정 녀석을 삭제할 수 있는 기능

 

urlSearchParams.delete(name)#

Remove all name-value pairs whose name is name.

[링크 : https://nodejs.org/api/url.html#url_urlsearchparams_delete_name]

Posted by 구차니
Programming/node.js2019. 4. 17. 18:27

간단하게 말하면 / 가 붙으면 directory

없으면 file로 간주하고 그에 따라 행동이 달라진다는 내용

[링크 : https://djkeh.github.io/articles/Why-do-we-put-slash-at-the-end-of-URL-kor/]

 

근데.. 클라이언트나 서버에서 임의로 /를 붙여서 해석하다 보니

어떤곳에서는 /가 붙고 어떤것에서는 /가 안 붙는데

요즘에는 RESTful API에 의해서

/는 단순 구분자가 되었고

/api/stone/

/api/stone/apply

식으로 꼬리에 꼬리를 물게 구성가능하다 보니

기존의 디렉토리, 파일 구조로 캐싱하기 쉽지가 않다..

 

+

node.js 에서 끝에 /로 끝나는 것 찾기

[링크 : https://stackoverflow.com/questions/6165381/how-to-get-the-last-part-of-a-string-in-javascript/6165387]

Posted by 구차니
Programming/node.js2019. 4. 16. 18:21

'Programming > node.js' 카테고리의 다른 글

nodejs url param delete  (0) 2019.04.17
url 끝의 /  (0) 2019.04.17
크롬에서 "사용가능한 소켓 대기중" 상태표시줄 메시지  (0) 2019.04.16
proxy error: Error: write after end  (0) 2019.04.15
https cache proxy using node.js  (0) 2019.04.12
Posted by 구차니
Programming/node.js2019. 4. 16. 12:41

프록시 갈군다고 크롬으로 하다 보니.. 아래에서 자꾸 먼가 뜨는데

찾아보니.. 지금도 그런진 모르겠지만 크롬은 소켓 6개까지 열수 있다고

그래서 ajax 등으로 소켓이 닫히지 않으면 이런식으로 대기가 걸리는 고질적인 문제가 있다고 한다.

프록시에서 그럼 응답을 못 받으면 헤어나질 못하게 되는건가?

 

[링크 : https://dev18.tistory.com/35]

[링크 : https://dev.uhoon.co.kr/entry/waiting-for-available-sockets-사용-가능한-소켓-대기-중-에러-원인과-확인]

 

+

아래 링크로 가면 관리 페이지가 뜨긴 하는데 상세 소켓별 상태를 볼 수 있는건 아님

[링크 : chrome://net-internals/#sockets]

'Programming > node.js' 카테고리의 다른 글

url 끝의 /  (0) 2019.04.17
js array key 삭제하기  (0) 2019.04.16
proxy error: Error: write after end  (0) 2019.04.15
https cache proxy using node.js  (0) 2019.04.12
node.js mkdir -p 옵션 사용하기  (0) 2019.04.10
Posted by 구차니
Programming/node.js2019. 4. 15. 18:20

에러는 뜨는데 어떻게 해결해야 할지 감이 안오네..

 

[링크 : https://stackoverflow.com/questions/27769842/write-after-end-error-in-node-js-webserver]

Posted by 구차니
Programming/node.js2019. 4. 12. 17:10

제목만 거창해지고 있네 ㅋㅋ

fs.mkdirSync(path, {recursive:true}); 로 해보려는데

1개 까진 생성해도 2개 깊이는 생성이 안되서 mkdirp 라는 모듈을 사용해서 해결

(아니 그럼.. parent 생성기능이 안되는 거잖아?)

[링크 : https://www.npmjs.com/package/mkdirp]

 

그리고 request가 아니라 response를 해주어야 하는거라 저장용도로는 일단 되는 듯

이걸 어떻게 request에 묶어서 다시 보내주지?

+

context에서 clientresponse를 통해 보내면 되는 듯.

ctx.proxyToClientResponse.end('Hacked, you cannot proceed to the website');

[링크 : https://github.com/joeferner/node-http-mitm-proxy/blob/master/examples/preventRequest.js]

 

var Proxy = require('http-mitm-proxy');
var proxy = Proxy();
var path = require('path');
var url = require('url');
var { URLSearchParams } = require('url');
var decode = require('urldecode')
var mkdirp = require('mkdirp');

var fs = require('fs');
var idx = 0;
var chunks = [];

proxy.use(Proxy.wildcard);

proxy.onError(function(ctx, err) {
                console.error('proxy error:', err);
                });

proxy.onRequest(function(ctx, callback) {
                {
                ctx.use(Proxy.gunzip);
                var pback = ctx.clientToProxyRequest.url;

                var params = new URLSearchParams(url.parse(pback).query);
                params.sort();
                //console.log(url.parse(pback).pathname + '?' + decode(params.toString()));

                ctx.onResponseData(function(ctx, chunk, callback) {
                        chunks.push(chunk);
                        return callback(null, chunk);
                        });
                }

                ctx.onResponseEnd(function(ctx, callback) {
                                //  console.log('REQUEST END', (Buffer.concat(chunks)).toString());
                                console.log('REQUEST END');
                                // console.log(path.basename(oriname.pathname))
                                // console.log(path.dirname(oriname.pathname))

                                var oriname = url.parse(ctx.clientToProxyRequest.headers.host + ctx.clientToProxyRequest.url)
                                var dirname = 'save/' + path.dirname(oriname.pathname) + '/';
                                var filename = path.basename(oriname.pathname) + idx;
                                //              idx++;
                                console.log(dirname);
                                console.log(filename);
                                mkdirp.sync(dirname);
                                fs.writeFileSync(dirname + filename, Buffer.concat(chunks));
                                chunks = [];
                                return callback();
                                });
                return callback();
});

proxy.onCertificateRequired = function(hostname, callback) {
        return callback(null, {
keyFile: path.resolve('/ca/certs/', hostname + '.key'),
certFile: path.resolve('/ca/certs/', hostname + '.crt')
});
};
proxy.listen({port: 3128});

 

 

 

[링크 : https://www.npmjs.com/package/http-mitm-proxy]

Posted by 구차니