Просмотр исходного кода

creat method which rest api each path

yusuketk 7 лет назад
Родитель
Сommit
11210894ee
1 измененных файлов с 19 добавлено и 15 удалено
  1. 19 15
      lib/util/restQiitaAPI.js

+ 19 - 15
lib/util/restQiitaAPI.js

@@ -4,11 +4,10 @@
 
 const https = require('https');
 const team = 'team';
-const token = 'API token';
-const options = {
+const token = 'api token';
+var options = {
   protocol: 'https:',
   host: `${team}.qiita.com`,
-  path: '/api/v2/items',
   method: 'GET',
   headers: {
     'Content-Type': 'application/json',
@@ -16,17 +15,22 @@ const options = {
   }
 };
 
-const req = https.request(options, (res) => {
-    res.on('data', (chunk) => {
-        console.log(`BODY: ${chunk}`);
-    });
-    res.on('end', () => {
-        console.log('No more data in response.');
-    });
-})
+function restAPI(path) {
+  options.path = `/api/v2/${path}`;
+  const req = https.request(options, (res) => {
+      res.on('data', (chunk) => {
+          console.log(`BODY: ${chunk}`);
+          return chunk;
+      });
+  })
 
-req.on('error', (e) => {
-  console.error(`problem with request: ${e.message}`);
-});
+  req.on('error', (e) => {
+    console.error(`problem with request: ${e.message}`);
+  });
 
-req.end();
+  req.end();
+};
+
+return {
+  items: restAPI('items')
+}