-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverCekOngkir.js
147 lines (116 loc) · 3.65 KB
/
serverCekOngkir.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// server.js
// BASE SETUP
// =============================================================================
// call the packages we need
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var http = require("http");
var qs = require("querystring");
// set your key rajaongkir.com
var key = '9f13553065a3291fb5d719d18f3669ee';
var path = require('path');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
var options = {
"method": "GET",
"hostname": "api.rajaongkir.com",
"port": null,
"path": "",
"headers": {
"key": key,
"Content-Type" : 'application/x-www-form-urlencoded'
}
};
//set cross domain
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET');
res.header("Access-Control-Allow-Headers", "X-Requested-With,Content-Type,Cache-Control");
if (req.method === 'OPTIONS') {
res.statusCode = 204;
return res.end();
} else {
return next();
}
});
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// set our port
var port = process.env.PORT || 8000;
// ROUTES FOR OUR API
var router = express.Router();
// get data province
router.get('/getprovinsi', function(req, response) {
options.method = 'GET';
options.path = '/starter/province';
//options.path = '/starter/city?province=5';//menampilkan kabupaten di provinsi ber-Id
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var provinsi = JSON.parse(chunks);
console.log(provinsi.rajaongkir.results);
//sekarang latihan membuat cache
//response.render('latihan',{
// listProvinsi : provinsi.rajaongkir.results
//});
});
});
req.end();
});
router.get('/getongkir', function(req, response) {
options.method = 'POST';
options.path = "/starter/cost";
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var ongkir = JSON.parse(chunks);
console.log(ongkir.rajaongkir.results[0].costs[0].cost[0].value);
});
});
req.write(qs.stringify({
origin: '501',
destination: '114',
weight: 1700,
courier: 'jne'
}));
req.end();
});
// get data cost
router.get('/cost/:origin/:destination/:weight', function(req, response) {
options.method = 'POST';
options.path = '/api/starter/cost';
var self = this;
self.origin = req.params.origin;
self.destination = req.params.destination;
self.weight = req.params.weight;
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
response.json(JSON.parse(body.toString()))
});
});
req.write(qs.stringify({ origin: self.origin,
destination: self.destination,
weight: self.weight
}));
req.end();
});
// all of our routes will be prefixed with /api/v1
var version = 'v1';
app.use('/', router);
//start the server
app.listen(port);
console.log('Server running on port ' + port);