-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathexpress.js
58 lines (49 loc) · 1.56 KB
/
express.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
/* External Package imports */
const express = require('express');
const http = require('http');
const multer = require('multer');
AutoLoad = require('@njs2/base/base/autoload.class');
AutoLoad.loadConfig();
AutoLoad.loadModules();
const { Executor } = require("@njs2/base");
/* External Package imports */
const app = express();
const upload = multer();
const server = http.createServer(app);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(upload.any());
/* eslint-disable-next-line no-unused-vars */
app.get('/postman', (req, res) => {
res.send(require('./postman.json'));
});
app.all("*", async (req, res) => {
// Neutralize input parameter received from express for Executor.executeRequest
let executorReq = {};
executorReq.httpMethod = req.method;
executorReq.queryStringParameters = req.query;
executorReq.body = req.body;
executorReq.pathParameters = {
proxy: req.path.length ? req.path.slice(1) : req.path
};
executorReq.headers = req.headers;
if (req.files) {
req.files.forEach(file => {
executorReq.body[file.fieldname] = {
type: 'file',
filename: file.originalname,
contentType: file.mimetype,
content: file.buffer
}
});
}
const executor = new Executor();
const result = await executor.executeRequest(executorReq);
return res.send(result);
});
const { API_PORT, API_ENDPOINT } = process.env;
// Start the server
server.listen(API_PORT, () => {
console.log(`App listening on port ${API_PORT}`);
console.log(`Postman endpoint: ${API_ENDPOINT}/postman`);
});