-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
84 lines (62 loc) · 2.19 KB
/
index.js
File metadata and controls
84 lines (62 loc) · 2.19 KB
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
const express = require('express');
const FormData = require('form-data');
const dotenv = require('dotenv');
const fs = require('fs');
const path = require('path');
const { apiHelper,createAxiosConfig } = require('./src/helper/apiHelperModule');
dotenv.config();
const app = express();
const port = process.env.PORT;
const hostname = process.env.hostname
const apiUrl = process.env.apiendpoint
app.listen(port | 5000,()=>{
console.log("server started on port "+port);
});
app.use(express.json());
app.post('/submitApplication',async (req,res)=>{
console.log('submitApplication');
const resPath = path.join(__dirname,'./resources');
console.log("pathname: "+resPath);
//Get a List of Numbers to Sum
const numReqConfig = createAxiosConfig("GET",`http://${hostname}:${port}/getNumberSum`,{},{});
const {data}= await apiHelper(numReqConfig);
console.log(data);
const jsonFormReq = jsonCreator(data);
console.log(jsonFormReq);
//create multipart request with given params
const formData = new FormData();
formData.append('application',JSON.stringify(jsonFormReq),{contentType:'application/json'});
formData.append('file',fs.createReadStream(resPath+'/test.pdf'),{contentType:'appliation/octet-stream'});
formData.append('source',fs.createReadStream(resPath+'/test.zip'),{contentType:'appliation/octet-stream'});
const headers = {
...formData.getHeaders()
}
const config = createAxiosConfig("POST",apiUrl,formData,headers);
const response = await apiHelper(config);
res.send(response.data);
})
app.get('/getNumberSum',async (req,res)=>{
const config = createAxiosConfig("GET",apiUrl,{},{});
const {data} = await apiHelper(config);
console.log(data);
const result = data.nums.reduce((prev,current)=> prev+current,0)
console.log(result);
res.send({
"sum":result,
"id":data?.id
});
})
const jsonCreator = ({sum,id})=>{
return {
"applicant": {
"firstName": "dummy",
"lastName": "dummy"
},
"role": "Fullstack Developer",
"referrer": "abc",
"answer": {
"questionId": id,
"sum": sum
}
}
}