Laravel 6 - Route post not found

Viewed 341

I'm trying to implement Register User in NuxtJS. However, I'm having this issue like below

vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php

In My api Route

 Route::post('register', 'V1\Auth\LoginController@register');

I'm try change it to get method and access it by 127.0.0.1:8000/api/register its work pretty fine.

i still have no idea, why i got this POST http://127.0.0.1:3000/api/register 404 (Not Found)

In My Nuxtjs

           async registerUser() {
                try {
                    await this.$axios.post(`api/register`, this.auth);
                    await this.$auth.login({
                        data: {
                            email: this.auth.email,
                            password: this.auth.password,
                        }
                    })
                    .then(res => {
                        console.log(res);
                        this.loading = false
                        // this.$router.push(`/auth/login`);
                    })
                    .catch(err => {
                        this.loading = false
                        this.$refs.form.validate(err.response.data.errors)
                        console.log(err.response);
                    })
                } catch(e) {
                    // statements
                    console.log(e);
                }

In My Controller

    public function register(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'email' => 'required|email',
            'password' => 'required|between:6, 25',
        ]);

        $user = new User();
        $user->name = $request->name;
        $user->email = $request->email;
        $user->password = bcrypt($request->password);
        $user->save();

        return response()->json(['registered' => true ]);
    }

I'll appreciate of all your Help.
Thanks.

2 Answers

You are using 3000 port http://127.0.0.1:3000/api/register in the axios, but the laravel use 8000 http://127.0.0.1:8000/api/register

You have to establish the API base URL In NuxtJS APP.

The reason is that Your NuxtJS app set default it's own base URL But you have to configure backend URL for laravel API.

Example,

Try This

await this.$axios.post(`http://127.0.0.1:8000/api/register`, this.auth);
Related