0

I have never worked with Dango Api.I am also new to node.js.I am trying to hit a link (Route) of my Laravel app with my Node.js.I am using Dango Api in my Laravel app.I heve tried to show data to the UI (node.js port) and print that data to console.But i am getting blank page in the screen and 'Undefined" in console.I am totally new in API.So,i don't even know if it will work.So please pardone me if this question is not appropriate as per the standard of this community.

Here is my api route-

    $api = app('Dingo\Api\Routing\Router');

    $api->version('v1', function ($api) {

    $api->get('/',function(){

    $user= User::where('id',1)->findOrFail();

    return response()->json($user);
  });


});

Here is my Node.js File:

var express = require('express');
var app=express ();
app.get('/api', function (req, res) {
res.send(req.user);
console.log(req.user);
})

var server = app.listen(8081, function () {
   var host = 'localhost'
   var port = 8000

   console.log(host,port);
})

Now,I am getting this error on the localhost:8081

 Cannot GET /
Asm Arman
  • 359
  • 6
  • 24
  • You should only define your routes in the routes file, not return responses and do queries, that belongs in the controller. I suggest you start by reading the (very good) Laravel doco about routes and controllers. In your routes, you point to a method in your controller, that's how it knows what to do (here it would be whatevercontroller@welcome) When that's all done make sure Dingo is set up properly (you might need some environment variables in your .env). You can test your API pretty easily with something like POSTMAN. check out https://laravel.com/docs/5.2/ – Ben Dubuisson Oct 05 '16 at 01:03
  • I have tried to point to a controller.= method.But I am getting an error.So,i use this.I don't think it's a wrong way to get data. – Asm Arman Oct 05 '16 at 04:15
  • I think you need to setup a very basic route, a very basic controller and method that returns something simple and build from there. You need to understand the basic concepts, because you're going in the wrong direction here. logic does not belong in routes but in your controller. – Ben Dubuisson Oct 06 '16 at 03:22
  • Do you know anything about Dingo api?If not,why wasting time here? @Ben Dubuisson – Asm Arman Oct 08 '16 at 17:19
  • Mate, yes I use Dingo API but if you can't get the Laravel basics right, you're going nowhere. Your comment is insulting, I'm trying to help. – Ben Dubuisson Oct 09 '16 at 19:44

1 Answers1

0

It doesn't look like you have a route declared for /api which you are trying to hit in Node.

You have only declared a route for the root ('/').

Try declaring a route for /api. You can also set a prefix for your API in your .env file if you want:

API_VERSION=v1
API_PREFIX=api
Ben Dubuisson
  • 727
  • 13
  • 38