-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
53 lines (43 loc) · 1.39 KB
/
index.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
require('dotenv').config()
const express = require('express');
const cors = require('cors');
const app = express();
const port = process.env.PORT || 3500;
// Configure CORS options
const corsOptions = {
origin: 'https://bajajfinserv-challenge1.onrender.com', // Allow this origin
methods: 'GET,POST',
allowedHeaders: 'Content-Type',
};
app.use(cors(corsOptions));
app.use(express.json());
// POST endpoint
app.post('/bfhl', (req, res) => {
const { data } = req.body;
if (!Array.isArray(data)) {
return res.status(400).json({ is_success: false, message: 'Invalid data format' });
}
const numbers = data.filter(item => !isNaN(item) && item.trim() !== '');
const alphabets = data.filter(item => /^[a-zA-Z]$/.test(item));
const highestLowercaseAlphabet = alphabets
.filter(item => item === item.toLowerCase())
.sort()
.pop() || '';
const response = {
is_success: true,
user_id: process.env.USER_ID,
email: process.env.EMAIL_ID,
roll_number: process.env.REG_NO,
numbers,
alphabets,
highest_lowercase_alphabet: highestLowercaseAlphabet ? [highestLowercaseAlphabet] : []
};
res.json(response);
});
// GET endpoint
app.get('/bfhl', (req, res) => {
res.status(200).json({ operation_code: 1 });
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});