-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay8.js
36 lines (28 loc) · 818 Bytes
/
Day8.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
const express = require('express')
const app = express()
function errorHandler(err, req, res, next) {
if (err instanceof PositiveIntegerError) {
res.status(400).json({ error: err.message });
} else {
next(err);
}
}
class PositiveIntegerError extends Error {
constructor(message) {
super(message);
this.name = 'PositiveIntegerError';
}
}
// Express route with positive integer validation
app.get('/positive', (req, res, next) => {
const number = parseInt(req.query.number);
if (Number.isInteger(number) && number > 0) {
res.json({ message: 'Success' });
} else {
next(new PositiveIntegerError('Number must be a positive integer.'));
}
});
app.use(errorHandler);
const port = process.env.PORT || 4000
app.listen(4000, () =>
console.log(`port is running on ${port}`));