내가 이해하지 못하는거야 무엇 하는 index.jsNodeJS
var server = require('./server');
server.start();
에서 다음 server.js
var http = require('http');
function start() {
function onRequest(request, response) {
console.log('onrequest called');
response.writeHead(200, { 'Content-type': 'text/plain' });
response.write("Hello world!");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server started!");
}
exports.start = start;
코드 그리고 그 다음은 어떻게 한 라인 exports.start = start;
가 작동 중입니다. exports
은 어디에서 오는가? 왜 index.js가 exports.start()
이 아닌 server.start();
의 시작 메소드를 호출합니까? exports
은 전역 변수 exports
에 대한 속성으로 다른 모듈에 액세스 할 수있는 로컬 변수를 만들기 위해 글로벌 네임 스페이스에 넣는 변수가 아닌가?
도움말!
'exports'된다 module.exports' '와 같은 객체를 의미한다. 그냥'start' 속성에 함수'start'를 내보내므로 다른 모듈에서 호출 할 수 있습니다. – Li357
@AndrewLi, 네 말이 맞아. https://nodejs.org/docs/latest/api/globals.html#globals_exports –
'exports'는 명령문이 아닙니다. 규칙적인 JS 객체 일뿐입니다 – Li357