Node.js Application Node.js应用-CarlZeng
Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight
and efficient. The Node.js package ecosystem, npm, is the largest ecosystem of open source libraries in the world.
Installation
Instructions for running Node program on Windows server
Download node https://nodejs.org/en/download/
Create code, I.e.
1
console.log('hello world');
Go to folder via command line
node start.js
var requirejs = require(“requirejs”); var argv = require(“minimist”)(process.argv.slice(2));
requirejs.config({nodeRequire: require });
requirejs([“fs”, “mkdirp”, “path”, “colors”,”./Constants”], function (fs, mkdirp, path, colors, constants){ “use strict”; function printHelp() {
console.log(“User Commands: \n\n”.gray);
} if (argv.help || argv.h || Object.keys(argv).length===1) {
printHelp(); return; } if (argv.email || argv.e) {
constants.email = (argv.email) ? (argv.email) : (argv.e);
} else {
console.log (“email argument is required.”); return;
} var sm = new Soap();
});
Login and SSOLogin are very similar XML. The function below returns the cookie header array. // login to the NS web service using the ssoLogin token // return the Set-Cookie header for subsequent requests because it contains the JSESSIONID
function sso_login(ws_url, ws_token)
{ var account = nlapiGetContext().getCompany(); var ret_val = null; var headers = {};
headers[‘Content-Type’]= ‘application/soap+xml’;
headers[‘SOAPAction’] = ‘ssoLogin’; var sso_login_xml = ‘<soap:Envelope xmlns:xsd=”http://www.w3.org/2001/XMLSchema“ xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/“ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance">‘ +
‘soap:Body‘ +
‘
‘
‘
‘
‘
‘
‘
‘‘+
‘‘; var login_response = nlapiRequestURL(ws_url_ver(ws_url,’NetSuitePort_2013_1’), sso_login_xml, headers); if(login_response){
ret_val = login_response.getHeaders(‘Set-Cookie’);
nlapiLogExecution(‘DEBUG’, ‘cookies returned’, join_cookies(ret_val)); if(g_Devmode){ var hdrs = login_response.getAllHeaders(); for (var i=0; i<hdrs.length; i++){
nlapiLogExecution(‘DEBUG’, hdrs[i] , login_response.getHeader(hdrs[i]));
}
}
} return ret_val;
} // Call API – pass in the cookie array set_cookie
function call_ws(xml, soap_action, sUrl, set_cookie){ //Setting up Headers
var headers = new Array();
headers[‘User-Agent-x’] = ‘SuiteScript-Call’;
headers[‘Content-Type’]= ‘application/soap+xml’;
headers[‘SOAPAction’] = soap_action; if (set_cookie){
headers[‘Cookie’] = join_cookies(set_cookie);
} var resp = nlapiRequestURL( url, xml , headers ); return resp.getBody();
} // assemble array for header to send back with next request
function join_cookies(cookie){ var joined = cookie[0]; for(var _i = 1;_i < cookie.length;_i++){
joined += “, “ + cookie[_i];
} return joined;
}
Instructions for running node program for on a Windows server
1. Download node https://nodejs.org/en/download/
2. Create code
3. Go to folder via command line
4. npm install
5. node start.js –email=
Note:
After Install node-v6.10.0.pkg, you will see:
Node.js was installed at /usr/local/bin/node
npm was installed at /usr/local/bin/npm
Make sure that /usr/local/bin is in your $PATH.