Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning JSON with Vue Router

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!

like image 272
Adam Avatar asked Oct 11 '17 23:10

Adam


1 Answers

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.

like image 191
Pascal Ganaye Avatar answered Oct 21 '22 11:10

Pascal Ganaye