I need to return a JSON object when users hit /nav on my application. This JSON is used programmatically and cannot be just a string.
Is there a way to make a vue component that is only JSON? Or a way to tell the router to return JSON?
Thank you!
I had a very similar problem. I wanted /nav to return JSON but only on my local development server however.
So I just created a vue.config.js
file (in my app root folder)
module.exports = {
devServer: {
before: function(app, server) {
app.get('/nav', function(req, res) {
const result = { x:1 , y : "hello" };
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(result));
});
}
}
}
With this files when I run npm run serve
, I get the json object when navigating to /nav
and my regular app otherwise.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With