2014-12-17 3 views
0

emailConfirmation.js에 모듈을 내보내는 방법 :Node.js를

var configAuth = require('../../authentication/sendgrid'); 
var sendgrid = require('sendgrid')(configAuth.sg.username, configAuth.sg.password); 

var from_address = "[email protected]"; 
var text_body = "sometextbody"; 
var html_body = "somehtml"; 

그들을 내가 같이, 포스트 경로에서 사용하기, 내 경로에 수출을해야합니다

app.post('/', function(req, res) { 

    sendgrid.send({ 
     to:   req.body.email, 
     from:  the from_adrres variable from the other file, 
     subject: "Some subjec", 
     text:  the text_body variable from the other file 
     html:  the html_body variable from the other file 
    }, function(err, json) { 
     if (err) { 
      return console.error(err); 
     } 
     console.log(json); 
}); 


}); 

을 내가 어떻게 할 수있는 emailConfirmation.js를 내보내고 그런 식으로 사용 하시겠습니까 ??

답변

0

코드에 맞는 파일을 만드는 것으로 시작하십시오.

파일의 끝에 코드의 일부가 소비자에게 노출됩니다. 예를 들어 : 귀하의 경우

당신과 같이 코드를 래핑 수 :

module.exports.set = function(app) { 
    app.post('/', function(req, res) { 
     /* the post code goes here */ 
    }); 
}; 

당신은 app.js에과 같이 app.js로 소비 수 :

require('./sendgrid').set(app); 

이 효과적으로 것 sendgrid에 대한 경로를 설정하십시오.

+0

안녕하세요 @ 제이슨, 답변 주셔서 감사합니다. .. 내가 더 잘 이해할 수있는 질문을 업데이트합니다. 확인해 드릴 수 있습니까? –

+2

내 마음 속에는 익명의 유권자가있는 특별한 장소가 있습니다. –

+0

ahahahahah .. 감사합니다 @ 제이슨 :) –