-
Notifications
You must be signed in to change notification settings - Fork 0
/
ms_APITool.js
164 lines (127 loc) · 4.34 KB
/
ms_APITool.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const express = require('express');
const fs = require('fs');
var path = require('path');
var cors = require('cors')
const axios = require('axios');
const fetch = require('node-fetch');
var bodyParser = require('body-parser');
const app = express();
app.use(express.static('./')) ;
app.use(cors()) ;
app.use(express.json({limit: '50mb'}));
// configure the app to use bodyParser()
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
const userID = '[email protected]' ;
const port = 9991 ;//parseInt(process.env.YouTubePort);
console.log(`port:${port}`) ;
app.listen(port, () => {
console.log(`helloworld: listening on port ${port}`);
});
app.post('/APIRequest.V1/', APIRequestV1) ;
let jsonRequest={
url:'http://127.0.0.1:9989/demoAPI',
action:'post',
content:{
say:'hello'
}
}
async function APIRequestV1(request, response) {
let jsonRequest = request.body ;
console.log(jsonRequest) ;
let todo = {
userId: 123,
title: "loren impsum doloris",
completed: false
};
//const json = await fetch(url).then(res => res.json())
let jsonResult = await fetch(jsonRequest.url, /*'https://jsonplaceholder.typicode.com/todos', */{
method: 'POST',
body: JSON.stringify(jsonRequest.content)/*JSON.stringify(todo)*/,
headers: { 'Content-Type': 'application/json' }
}).then(res => res.json()) ;
console.log(jsonResult) ;
let jsonResponse={
retCode:'200',
content:jsonResult
}
response.send(jsonResponse) ;
/*.then(res =>{
console.log(res) ;
res.json() ;
}).then(json=>{
console.log(json);
let jsonResponse={
retCode:'200',
content:json
}
response.send(jsonResponse) ;
}).catch(error=>{
console.log(error) ;
});
*/
}
//https://stackabuse.com/making-http-requests-in-node-js-with-node-fetch/
function requestAPI(jsonRequest){
let todo = {
userId: 123,
title: "loren impsum doloris",
completed: false
};
fetch('https://jsonplaceholder.typicode.com/todos', {
method: 'POST',
body: JSON.stringify(todo),
headers: { 'Content-Type': 'application/json' }
}).then(res => res.json())
.then(json => console.log(json));
}
/*
app.get('/fetchPendingToDo.V1/', fetchPendingToDoV1) ; //http://127.0.0.1:9990/fetchPendingToDo.V1/
app.get('/fetchPendingToDosOf.V1/:project', fetchPendingToDosOfV1) ; //http://127.0.0.1:9990/fetchPendingToDosOf.V1/:Penguin.V3
app.post('/newToDo.V1/', newToDoV1) ;
app.post('/doneWithToDo.V1/', doneWithToDoV1) ;
async function fetchPendingToDoV1(request, response) {
let jsonToDos = await libToDos.fetchAllPendingToDos() ;
response.json(jsonToDos) ;
}
async function fetchPendingToDosOfV1(request, response) {
const {project } = request.params;
let cProject = project.replace(':','') ;
let jsonToDos = await libToDos.fetchPendingToDosOf(cProject) ;
response.json(jsonToDos) ;
}
async function doneWithToDoV1(request, response) {
let jsonToDo = request.body ;
//console.log(`updateGatewayV1:${jsonData}`) ;
//console.log(JSON.stringify(jsonData,null,3)) ;
await libToDos.doneWithToDo(jsonToDo) ;
response.send({ retCode: '200' }) ;
}
async function fetchAllPendingEventsV1(request, response) {
let jsonEvents = await libCalendar.fetchAllPendingEvents() ;
console.log(jsonEvents) ;
response.json(jsonEvents) ;
}
async function fetchEventV1(request, response) {
const {id } = request.params;
let cID = id.replace(':','') ;
console.log(`fetchEventV1 for ${cID}`) ;
let jsonEvent = await libCalendar.fetchEvent(parseInt(cID)) ;
console.log(jsonEvent) ;
response.json(jsonEvent) ;
}
async function newCalendarEventV1(request, response) {
let jsonEvent = request.body ;
//console.log(`updateGatewayV1:${jsonData}`) ;
//console.log(JSON.stringify(jsonData,null,3)) ;
await libCalendar.newCalendarEvent(jsonEvent) ;
response.send({ retCode: '200' }) ;
}
async function doneWithEventV1(request, response) {
let jsonEvent = request.body ;
//console.log(`updateGatewayV1:${jsonData}`) ;
//console.log(JSON.stringify(jsonData,null,3)) ;
await libCalendar.doneWithEvent(jsonEvent) ;
response.send({ retCode: '200' }) ;
}
*/