xxxxxxxxxx
const express = require('express');
const app = express();
app.get('/', (req, res) => {
// Example code where a request parameter is expected
const paramValue = req.query.param;
if (!paramValue) {
res.status(400).send('Bad Request - Missing parameter');
} else {
// Continue with the normal flow of the application
// ...
res.send('Success!');
}
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
xxxxxxxxxx
fetch('https://example.com/api/endpoint', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
// Request payload
})
})
.then(response => {
if (response.ok) {
// Request was successful
return response.json();
} else if (response.status === 400) {
// Bad Request
throw new Error('Invalid request. Please check your parameters.');
} else {
// Other error codes
throw new Error('Something went wrong.');
}
})
.then(data => {
// Process response data
})
.catch(error => {
// Handle and display the error
console.error(error);
});