[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / core_nodes / deprecated / 90-httpget.js
1 /**
2  * Copyright 2013 IBM Corp.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  **/
16
17 var RED = require(process.env.NODE_RED_HOME+"/red/red");
18
19 function HttpGet(n) {
20     RED.nodes.createNode(this,n);
21     this.warn("This node has been deprecated and will be deleted in a future release. Please update your flow to use the 'http request' node.");
22     this.baseurl = n.baseurl || "";
23     this.append = n.append || "";
24     var node = this;
25     if (this.baseurl.substring(0,5) === "https") { var http = require("https"); }
26     else { var http = require("http"); }
27     this.on("input", function(msg) {
28         msg._payload = msg.payload;
29         //util.log("[httpget] "+this.baseurl+msg.payload+this.append);
30         http.get(this.baseurl+msg.payload+this.append, function(res) {
31             node.log("Http response: " + res.statusCode);
32             msg.rc = res.statusCode;
33             msg.payload = "";
34             if ((msg.rc != 200) && (msg.rc != 404)) {
35                 node.send(msg);
36             }
37             res.setEncoding('utf8');
38             res.on('data', function(chunk) {
39                 msg.payload += chunk;
40             });
41             res.on('end', function() {
42                 node.send(msg);
43             });
44         }).on('error', function(e) {
45             //node.error(e);
46             msg.rc = 503;
47             msg.payload = e;
48             node.send(msg);
49         });
50     });
51 }
52
53 RED.nodes.registerType("httpget",HttpGet);