Skip to content

Commit 88486dd

Browse files
daeyeonyichoi
authored andcommitted
Add http samples (#1004)
IoT.js-DCO-1.0-Signed-off-by: Daeyeon Jeong [email protected]
1 parent a542fb4 commit 88486dd

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

samples/http_hello/client_get.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* Copyright 2017-present Samsung Electronics Co., Ltd. and other contributors
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
var http = require('http');
17+
var options = {
18+
hostname: '127.0.0.1',
19+
port: 8080,
20+
path: '/'
21+
};
22+
23+
http.request(options, function (res) {
24+
receive(res, function (data) {
25+
console.log(data);
26+
});
27+
}).end();
28+
29+
function receive(incoming, callback) {
30+
var data = '';
31+
32+
incoming.on('data', function (chunk) {
33+
data += chunk;
34+
});
35+
36+
incoming.on('end', function () {
37+
callback ? callback(data) : '';
38+
});
39+
}

samples/http_hello/client_post.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* Copyright 2017-present Samsung Electronics Co., Ltd. and other contributors
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
var http = require('http');
17+
18+
var message = JSON.stringify({
19+
greeting: 'Hello, IoT.JS!',
20+
answer: '',
21+
});
22+
23+
var options = {
24+
hostname: '127.0.0.1',
25+
port: 8080,
26+
path: '/',
27+
method: 'POST',
28+
headers: {
29+
'Content-Length': message.length
30+
}
31+
};
32+
33+
http.request(options, function (res) {
34+
receive(res, function (data) {
35+
var obj = JSON.parse(data);
36+
console.log(obj.answer);
37+
});
38+
}).end(message);
39+
40+
function receive(incoming, callback) {
41+
var data = '';
42+
43+
incoming.on('data', function (chunk) {
44+
data += chunk;
45+
});
46+
47+
incoming.on('end', function () {
48+
callback ? callback(data) : '';
49+
});
50+
}

samples/http_hello/server.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* Copyright 2017-present Samsung Electronics Co., Ltd. and other contributors
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
var http = require('http');
17+
var port = 8080;
18+
19+
http.createServer(function (req, res) {
20+
if (req.method == 'GET') {
21+
status(res, 'Hello, IoT.JS!');
22+
23+
} else if (req.method == 'POST') {
24+
receive(req, function (data) {
25+
var obj = JSON.parse(data);
26+
obj.answer = 'Hello, There!'
27+
status(res, obj);
28+
});
29+
}
30+
}).listen(port);
31+
32+
function receive(incoming, callback) {
33+
var data = '';
34+
35+
incoming.on('data', function (chunk) {
36+
data += chunk;
37+
});
38+
39+
incoming.on('end', function () {
40+
callback ? callback(data) : '';
41+
});
42+
}
43+
44+
function status(res, data) {
45+
var isJson = (typeof data === 'object');
46+
47+
if (isJson) {
48+
data = JSON.stringify(data);
49+
}
50+
51+
var headers = {
52+
'Access-Control-Allow-Origin': '*',
53+
'Access-Control-Allow-Headers':
54+
'Origin, X-Requested-With, Content-Type, Accept',
55+
'Content-Type': isJson ? 'application/json' : 'text/plain',
56+
};
57+
58+
res.writeHead(200, headers);
59+
res.end(data);
60+
};

0 commit comments

Comments
 (0)