Skip to content

Commit

Permalink
devex: Create an http server for cognito to forward its triggers to
Browse files Browse the repository at this point in the history
  • Loading branch information
Zachary Rogers committed Dec 7, 2023
1 parent e1770a7 commit 90a28f6
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions web-api/src/app-local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,39 @@ localPublicApiApp.listen(localPublicApiPort);
console.log(`Listening on http://localhost:${localPublicApiPort}`);

// ************************ cognito-junk *********************************
const cognitoApp = express();
cognitoApp.use(express.json({ limit: '1200kb' }));
cognitoApp.listen(9845);
cognitoApp.use('*', (req, res) => {
console.log('Cognito req.url', req.url);
console.log('Cognito req.method', req.method);
console.log('Cognito req.headers', req.headers);
console.log('Cognito req.body', req.body);
res.send();
const cognitoServer = http.createServer((req, res) => {
if (req.method === 'POST') {
let requestBody = '';

req.on('data', chunk => {
requestBody += chunk;
});

req.on('end', async () => {
// Parse the JSON body (assuming it's JSON)
try {
const data = JSON.parse(requestBody);
console.log('I AM DATA', { data });
await handler(data);
} catch (error) {
// If parsing fails, respond with a 400 Bad Request
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('400 Bad Request\n');
}
});
}

res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({
body: '',
statusCode: 200,
}),
);
});
cognitoApp.post('/', (req, res) => {
res.send(handler(req));

cognitoServer.listen(9845, () => {
console.log('Server running at http://localhost:9845/');
});

// ************************ streams-local *********************************
Expand Down

0 comments on commit 90a28f6

Please sign in to comment.