fetch('/categories') ¿? #13

Closed
opened 2020-10-25 08:14:01 +01:00 by Ghost · 10 comments

Why the line
fetch('/categories').then(res => res.json()).then(res => res.forEach(c => document.querySelector('#category').innerHTML += <option value="${c}">${c[0]}${c.slice(1).replace(/_/g, ' ').toLowerCase()}</option>));

it throws:
Failed to load resource: the server responded with a status of 404
So that the path (Categories/ does not exist on root

e.g. http://localhost:52558/categories

Why the line fetch('/categories').then(res => res.json()).then(res => res.forEach(c => document.querySelector('#category').innerHTML += `<option value="${c}">${c[0]}${c.slice(1).replace(/_/g, ' ').toLowerCase()}</option>`)); it throws: Failed to load resource: the server responded with a status of 404 So that the path (Categories/ does not exist on root e.g. http://localhost:52558/categories

Also throws error:
Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
Promise.then (async)
(anonymous) @ index.js:6

Also throws error: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 Promise.then (async) (anonymous) @ index.js:6
Owner

Something is not right with your setup.
Please restart from the beginning and tell me exactly how you're doing it.

Something is not right with your setup. Please restart from the beginning and tell me exactly how you're doing it.
Owner

Same as #15, /categories is not a static file but an HTTP route that you can find at index.js:13.

Same as #15, `/categories` is not a static file but an HTTP route that you can find at [`index.js:13`](https://git.kaki87.net/KaKi87/KPlaySearch/src/branch/master/index.js#L13).

STEPS that I do:
I installed NodeJs, Yean, Git
I Downloaded your files, extracted your project in:
C:\Production\KPlaySearch-master\kplaysearch\

I ran:
C:\Production\KPlaySearch-master\kplaysearch\yarn install

then:
C:\Production\KPlaySearch-master\kplaysearch>yarn start
yarn run v1.22.5
$ node index.js
Server running

Then when I run localhost in :
C:\Production\KPlaySearch-master\kplaysearch\public

Running index.html runs attached error:
index.js:6 GET http://localhost:56337/categories 404 (Not Found

And if I try to search say: "Explorer" I do not get any results and get:
index.js:34 POST http://localhost:56337/search 404 (Not Found)

So by the way, code as you provided by default does not work

STEPS that I do: I installed NodeJs, Yean, Git I Downloaded your files, extracted your project in: C:\Production\KPlaySearch-master\kplaysearch\ I ran: C:\Production\KPlaySearch-master\kplaysearch\yarn install then: C:\Production\KPlaySearch-master\kplaysearch>yarn start yarn run v1.22.5 $ node index.js Server running Then when I run localhost in : C:\Production\KPlaySearch-master\kplaysearch\public Running index.html runs attached error: index.js:6 GET http://localhost:56337/categories 404 (Not Found And if I try to search say: "Explorer" I do not get any results and get: index.js:34 POST http://localhost:56337/search 404 (Not Found) So by the way, code as you provided by default does not work
Owner

I run localhost in :
C:\Production\KPlaySearch-master\kplaysearch\public

What do you mean by that exactly ?

> I run localhost in : C:\Production\KPlaySearch-master\kplaysearch\public What do you mean by that exactly ?

I meant that setting up a website in IIS under the root directory of :

C:/ .... KPlaySearch(extractedprojectfiles)/public
(in my case e.g.: C:\Production\KPlaySearch-master\kplaysearch\public)

Dont work as show in pictures, say new attached picture

I meant that setting up a website in IIS under the root directory of : C:/ .... KPlaySearch(extractedprojectfiles)/public (in my case e.g.: C:\Production\KPlaySearch-master\kplaysearch\public) Dont work as show in pictures, say new attached picture
Owner

You're not supposed to do that.

fastify is a server of its own.

If you look at the code (index.js:27), you'll see that the script creates an HTTP server on port 3030.

I guess I could have put it in a config file or as an environment variable but I didn't used to bother doing that at the time.

So, you'll access it at http://localhost:3030/, and everything will work as expected.

As I said, /search and /categories are fastify routes, called by fetch requests, respectively in POST and GET, returning JSON data. Those are detailed in the main script.

The only folder containing static files is /public.

You're not supposed to do that. `fastify` is a server of its own. If you look at the code ([`index.js:27`](https://git.kaki87.net/KaKi87/KPlaySearch/src/branch/master/index.js#L27)), you'll see that the script creates an HTTP server on port `3030`. I guess I could have put it in a config file or as an environment variable but I didn't used to bother doing that at the time. So, you'll access it at `http://localhost:3030/`, and everything will work as expected. As I said, `/search` and `/categories` are *fastify* routes, called by *fetch* requests, respectively in POST and GET, returning JSON data. Those are detailed in the main script. The only folder containing static files is `/public`.
Owner

Plus, the log output Server running should have made you realize this ^^

Plus, the log output `Server running` should have made you realize this ^^

Done, using localhost:3030
But I cannot run on my own localhost used by Visual Studio 2019 (IIS based)

Done, using localhost:3030 But I cannot run on my own localhost used by Visual Studio 2019 (IIS based)
Owner

Well, it's not like PHP where whe have static files everywhere.
Welcome to Node.

Well, it's not like PHP where whe have static files everywhere. Welcome to Node.
This repository is archived. You cannot comment on issues.
No milestone
No project
No assignees
2 participants
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: playsearch.kaki87.net/v1#13
No description provided.