How to import a NodeJS module to apigee edge?

5 6 4,034

Question:

I want to create a proxy for sending Email on Apigee enterprise portal and I want to import the volos-mailer module. How can I do it?

To import node.js modules to Apigee Edge, please reference http://apigee.com/docs/management/apis/post/organizations/%7Borg_name%7D/apis/%7Bapi_name%7D/revisio... for details.

In order to install the volos-mailer module, the package.json file in the API proxy needs to list volos-mailer under the “dependencies" element.

Step-by-step instructions are shown below on how to create a simple sample proxy on Apigee enterprise portal to send emails through gmail.

1. Create a New Node.js proxy in Enterprise UI by clicking Node.js Sample "Hello World". Create with the name "hello-v1" and the Project Base Path “/v1/hello”. This is revision 1 and is deployed to test on creation. Please double check it runs well by running “curl 
<a href="http://kehyuehfu-test.apigee.net/v1/hello”">http://kehyuehfu-test.apigee.net/v1/hello”</a> or enter "http://kehyuehfu-test.apigee.net/v1/hello” in the browser.  You should see the “Hello, World!” output. 
2. Save the hello-v1 proxy as Revision 2 by going to Project -> Save as New Revision. 
3. In Revision 2, modify hello-world.js to have below code and click on Save.

var nodemailerConnector = require('volos-mailer');
 
var http = require('http');

var profile = 
{ 
	host: 'smtp.gmail.com', 
	port: '587', 
	auth: {"user":"user1@gmail.com","pass":"password"} 
};

var nodemailerConnectorObject = new nodemailerConnector.NodemailerConnector({"profile": profile});

var svr = http.createServer(function (req, resp) { nodemailerConnectorObject.dispatchRequest(req, resp); });

svr.listen(9089, function () { nodemailerConnectorObject.initializePaths(nodemailerConnectorObject.configuration.restMap); 

console.log(nodemailerConnectorObject.applicationName + ' node server is listening'); });

 4. In Revision 2, add package.json by clicking New -> New Script, then enter package.son as the Script Name and Node as the File Type. 
5. Enter the following to package.json and click on Save.
{ 
	"name": "testmail", 
	"version": "0.0.1", 
	"description": "Testing volos-mailer", 
	"main": "hello-world.js", 
	"scripts": { "start": "node hello-world.js" }, 
	"author": "Apigee", 
	"license": "", 
	"dependencies": { 
	  "http": "", 
	  "volos-mailer": "" } 
}
6. Run the import API, at <a href="http://apigee.com/docs/management/apis/post/organizations/%7Borg_name%7D/apis/%7Bapi_name%7D/revisions/%7Brevision_num%7D/npm,">http://apigee.com/docs/management/apis/post/organizations/%7Borg_name%7D/apis/%7Bapi_name%7D/revisions/%7Brevision_num%7D/npm,</a> to import the modules in listed in package.json. 

Or, run
curl -X POST <a href="http://apigee.com/docs/management/apis/post/organizations/%7Borg_name%7D/apis/%7Bapi_name%7D/revisions/%7Brevision_num%7D/npm,">http://apigee.com/docs/management/apis/post/organizations/%7Borg_name%7D/apis/%7Bapi_name%7D/revisions/%7Brevision_num%7D/npm</a>.

The request body is command=install. It would return with a 200 response code and the response would show a complete list of  dependency modules imported. 
7. Check in the enterprise portal that node_modules_http.zip and node_modules_volos-mailer.zip are showing up under Scripts in hello-v1. 
8. Deploy revision 2. This is a must-do step after the import. 
9. Run curl -X POST <a href="http://org1-test.apigee.net/v1/hello/mail">http://org1-test.apigee.net/v1/hello/mail</a> -d '{"from": “User1<user1@company.com>", "to”: "user1@company.com", "subject": “Just Saying Hi", "html": "<b>yes sir</b>"}' -H 'Content-Type: application/json'

and you should see something like this:

{
"action": "POST",
"query": {},
"body": {
"action": "POST",
"query": {},
"body": {
"from": "User1<user1@company.com>",

"to": "user1@company.com",

"subject": "Just Saying Hi",

"html": "<b>yes sir</b>"
},
"path": "/mail",
"url": "/mail",
"data": {
"accepted": [
“user1@gmail.com"

],
"rejected": [],
"response": "250 2.0.0 OK 1424132871 c26sm12757154yhb.20 - gsmtp",
"envelope": {
"from": “user1@company.com",
"to": [
“user1@company.com"
]
},
"messageId": "1424132871335-485e334b-b70c66c5-862983e1@yahoo.com"
},
"timestamp": 1424132871867,
"duration": 825,
"applicationName": "volos-mailer"

} 

or 

Run curl 'http://org1-test.apigee.net/v1/hello/mail?from=user1@company.com&to=user1@company.com&subject=Hello%20world&html=<b>Just%20saying%20hello!</b>’

{
"action": "GET",
"query": {
"from": “user1@company.com",

"to": “user1@company.com",

"subject": "Hello world",
"html": "<b>Just saying hello!</b>"
},
"path": "/mail",
"url": "/mail?from=user1%40company.com&to=user1%40company.com&subject=Hello%20world&html=%3Cb%3EJust%20saying%20hello%21%3C%2Fb%3E",
"data": {
"accepted": [
“user1@gmail.com"

],
"rejected": [],
"response": "250 2.0.0 OK 1424132832 o10sm12747707yho.51 - gsmtp",
"envelope": {
"from": “user1@company.com",

"to": [
“user1@company.com"

]
},
"messageId": "1424132831610-d1d179d3-bb20e082-a37e27a8@apigee.com"
},
"timestamp": 1424132832171,
"duration": 857,
"applicationName": "volos-mailer"

} 

you should see email coming to you in your inbox if you have the to field set to your email address.

Comments
Not applicable

Guys, Thanks a TON for this information. Looks like only article to show how to install a nodejs module properly

Not applicable

@Michael Malloy

could you please explain step number 6,9 with screenshots.

I am getting some error. PFA for reference.nodeerror.png

Thanks in advance

edu4krishanu
New Member

@shubham singh this means you are not following the steps properly. I got the same error and then I started from begining again and it was resolved. Make sure you don't put pacakge.json inside of any folder.

edu4krishanu
New Member

Hi @corinna fu, I have followed your process and was able to install the dependency which were mentioned in package.json. But doesn't install all the npm modules on which my required module dependent. Do you have a solution for that?

Thanks,

Krish

Not applicable

I have done upto step 6. Able to include the dependencies in the script. But again I am getting the same error.

Not applicable

I have done upto step 6. Able to include the dependencies in the script. But again I am getting the same error.

Version history
Last update:
‎02-16-2015 05:20 PM
Updated by: