Skip to content

Instantly share code, notes, and snippets.

@HamidMolareza
Last active November 12, 2023 21:56
Show Gist options
  • Save HamidMolareza/bc276b2649c1cb5d5a654abbcb837bd3 to your computer and use it in GitHub Desktop.
Save HamidMolareza/bc276b2649c1cb5d5a654abbcb837bd3 to your computer and use it in GitHub Desktop.
stackoverflow-77470486
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = 3000;
// Middleware to parse JSON in the query parameters
app.use(bodyParser.json());
app.get('/test', (req, res) => {
// Extract the 'data' query parameter
const rawData = req.query.Message;
try {
// Parse the JSON data
const jsonData = JSON.parse(rawData);
// Handle the parsed data
console.log('Parsed JSON Data:', jsonData);
// Respond with a success message
res.status(200).json({ message: 'Data successfully parsed and handled.' });
} catch (error) {
// Respond with an error if parsing fails
res.status(400).json({ error: 'Failed to parse JSON data.' });
console.error(error);
}
});
app.listen(port, () => {
console.log(`Server is running on http://127.0.0.1:${port}/`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment