Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

node.js - Node Js access to api laravel

Hi i already have one api in laravel, but i need access with express node js.

This is my funcition

var data = querystring.stringify({
    _token: 'LhTsymoueRtcWtjP69MD1KEbDyGl0NGuewWOieER',
    propertiesString: properties
});

var options = {
    hostname: 'apiproperties.local',
    port: 80,
    path: '/properties/storeSB',
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': Buffer.byteLength(data)
    }
    /*headers: {
      'Content-Type': 'application/json',
    }*/
};

var req = http.request(options, function(res) {
    res.setEncoding('utf8');
    console.log('Status: ' + res.statusCode);
    console.log('Headers: ' + JSON.stringify(res.headers));
    res.on('data', function (chunk) {
        console.log("body: " + chunk);
    });
});

req.write(data);
req.end();

This give me error Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php line 68 because i dont send _token, but i dont know how send token in nodejs i not have views in nodejs because i not want to use i not need it, so how send token in my post request without call for form in one view? regards

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should exclude your api written in Laravel from CSRF Protection check middleware by default VerifyCsrfToken middleware is applied to route group web so here you are having two options :-

  1. Create a new middleware group named api
    code snippet for creating a middleware
    routes.php

    Route::group(['prefix' => 'api/v1','middleware' => ['api']], function () { Route::get('/hotel/list',[ 'uses' => 'YourController@function' ]); });

    VerifyCsrfToken.php

    protected $except = [ 'api/v1/*', ];

  2. Directly exclude routes for CSRF check
    In VerifyCsrfToken.php add all api's url which you want to ignore for CSRF check

    class VerifyCsrfToken extends BaseVerifier { /** * The URIs that should be excluded from CSRF verification. * * @var array */ protected $except = [ 'url_regex' ]; }

First method is more suggested as for all future new routes addition would work out we just need to add that route under this middleware group.

Let me know in comments if it worked out or if have any query.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...