nodejs에서는 다양한 내장객체를 보유하고 있는데 그중하나인 process는 process를 관장하는 객체들을 관리하는 객체이다. 이중 process.env는 process의 환경변수를 설정한다. cmd에서 node에 접속해 process.env 객체를 실행하면, 사용자의 환경에서 설정된 환경변수들이 나타나게 된다.
이때 주의해야할 점은, 환경변수를 수정하는 것은 가능하지만, 그러한 변경요소는 nodejs 프로세스 외부에서는 반영되지 않는 다는 점이다. 즉, 수정한 환경변수는 nodejs 프로세스 내에서만 사용할 수 있다.
:The non-standard stack property of Error objects offer a trace of which functions were called, in what order, from which line and file, and with what arguments. The stack string proceeds from the most recent calls to earlier ones, leading back to the original global scope call.
Generator는 빠져나갔다가 나중에 다시 돌아올 수 있는 함수입니다. 이때 컨텍스트(변수 값)는 출입 과정에서 저장된 상태로 남아 있습니다.
Generator 함수는 호출되어도 즉시 실행되지 않고, 대신 함수를 위한 Iterator 객체가 반환됩니다. Iterator의 next() 메서드를 호출하면 Generator 함수가 실행되어 yield 문을 만날 때까지 진행하고, 해당 표현식이 명시하는 Iterator로부터의 반환값을 반환합니다. yield * 표현식을 마주칠 경우, 다른 Generator 함수가 위임(delegate)되어 진행됩니다.
이후 next() 메서드가 호출되면 진행이 멈췄던 위치에서부터 재실행합니다. next() 가 반환하는 객체는 yield 문이 반환할 값(yielded value)을 나타내는 value 속성과, Generator 함수 안의 모든 yield 문의 실행 여부를 표시하는 boolean 타입의 done 속성을 갖습니다. next() 를 인자값과 함께 호출할 경우, 진행을 멈췄던 위치의 yield 문을 next() 메서드에서 받은 인자값으로 치환하고 그 위치에서 다시 실행하게 됩니다.
function*idMaker(){var index =0;while(index <3)yield index++;}var gen =idMaker();
console.log(gen.next().value);// 0
console.log(gen.next().value);// 1
console.log(gen.next().value);// 2
console.log(gen.next().value);// undefined// ...
1. 리모트 디버깅을 사용할 에디터에 어느 서버에서 어느 포트를 이용해서 디버깅을 실행할지 컨피그파일 설정하기. 아래의 예는 visual studio code에 해당하는 것이고, 이 에디터는 프로젝트 안에 .vscode 폴더가 있으면 그 안에서 에디터 설정파일들을 찾아 읽는다. 디버깅 설정파일은 launch.json에 쓰여져 있다. 아래설정 파일에선 10.1.3.23서버의 3019포트를 디버깅 포트로 설정했다.
{
// Use IntelliSense to learn about possible Node.js debug attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "リモートポートへのアタッチ",
"address": "10.1.3.23",
"port": 3019,
"remoteRoot": "/home/ec2-user/dev/AI_HUB/",
"localRoot":"${workspaceRoot}"
}
]
}
2. 서버에서 노드 제이에스를 실행할시 --inspect 명령어를 옵션으로 추가한다.
NODE_ENV=unit-test node --inspect=3019 app.js
▶require는 새로운 인스턴스를 생성하는 함수가 아니다.
디버깅하다보면 require는 시스템이 구동되기전 우선적으로 먼저 불러지는 것을 알 수 있다. require은 새로운 인스턴스를 생성하는 것이 아니라, 단지 서로 다른 위치에 있는 파일들을 연결시켜주는 것을 뿐.따라서 새로운 인스턴스를 사용하고 싶다면 new해야한다.
▶http.createServer VS app.listen: 코드가 돌아가는 로컬을 서버의 역할을 하도록 하게 해준다.
여기까지보면, 이는 사실 express의 app 객체를 통해 app.listen(8080)하는 것과 차이가 없다. 도대체 뭐가 다른 걸까?
아래에 좋은 해답이 있어 긁어와봤다.(https://stackoverflow.com/questions/17696801/express-js-app-listen-vs-server-listen)
var express = require('express');var app = express();//app.configure, app.use etc
app.listen(1234);
and adding an http server:
var express = require('express');var http = require('http');var app = express();var server = http.createServer(app);//app.configure, app.use etc
server.listen(1234);
What's the difference?
The second form (creating an HTTP server yourself, instead of having Express create one for you) is useful if you want to reuse the HTTP server, for example to run socket.io within the same HTTP server instance:
var express = require('express');var app = express();var server = require('http').createServer(app);var io = require('socket.io').listen(server);...
server.listen(1234);
However, app.listen() also returns the HTTP server instance, so with a bit of rewriting you can achieve something similar without creating an HTTP server yourself:
var express = require('express');var app = express();// app.use/routes/etc...var server = app.listen(3033);var io = require('socket.io').listen(server);
io.sockets.on('connection',function(socket){...});
▶res.sendStatus와 res.status().send의 차이
res.sendStatus는 단순하게 상태 번호만 전송해주고, res.status().send는 상태번호와 레스폰스를 동시에 보낼 수 있다.
res.sendStatus(200);// equivalent to res.status(200).send('OK')
res.sendStatus(403);// equivalent to res.status(403).send('Forbidden')
res.sendStatus(404);// equivalent to res.status(404).send('Not Found')
res.sendStatus(500);// equivalent to res.status(500).send('Internal Server Error')