17
M1sf3t
91d

.... don't judge me. I'm not proud 😔

Comments
  • 3
    what the hell, we get downvotes for a wall of shame now?
  • 4
    Add some typescript :p
  • 9
    Use axios?
  • 2
    @alexbrooklyn I'm allergic to microsoft 😅
  • 5
    What are we supposed to judge? Your api calling another api?
    I had to do this too in a project. I had to make a progressive web app, and those cant make calls with http. Only https.
    The only improvement id make is introducing cache if the data does not change a lot.
  • 1
    @devTea That would definitely be much easier, but I was attempting to learn to do this without the help of a package. Figured it might help me get a little better idea of what's going on when I did use axios, but I didn't realize I was going to have to refer to ten separate chapters of the node manual just to make an attempt.
  • 5
    @M1sf3t vannila fetch is similar to axios,

    Also typescript is more like static typing not really M$ fault tho
  • 1
    @24th-Dragon the JSON.stringify( JSON.parse( data )) part. Seems kinda hacky but so far it's the only way I've been able to get it to return anything other than a buffer with a bunch of random numbers.

    I'm about to go back and replace that data push with data += though and see what I can do with that. That's the way I was trying to do it originally and it was giving me json object back but at the time my client call wasn't waiting on the server call and it got changed somewhere in the process of getting that part to work.
  • 1
    @devTea that's what I'm making the call from the client side with. this.fetch anyway, not sure exactly what the difference is but that's what the sapper docs said to use.

    I don't think I realized you could use fetch on the backend, axios and request are the only examples I can find other than what's in the node docs.
  • 1
    ugh you have got to be fucking kidding me. Changed data.push(d) to data += d and removed both the stringify and parse from the res.end and now it works. I don't even have to have body parser plugged in, it's converting it to an object when it hits the client all on its on.

    Remind me to never bitch about css again 🤦‍♂️
  • 1
    is there no res.json() or something?
  • 1
    @alexbrooklyn yea but I wasn't entirely sure what that did. the example for server routes has it but they were using an imported database file and every other example I could find on the web concerning third party api's at minimum had body parser plugged in. Most were using ejs for rendering though so that may have had something to do with it. Some of the sapper guys have it plugged into their stuff as well. For what I haven't a clue, maybe posts?
  • 3
    All im gonna judge you for is that God awful editor
  • 1
    @shoop I have no patience for the fancier ones 🤷‍♂️
  • 2
    It's not the worst thing I've seen today, so you got that going for you 😉

    And yes the -- is a thing now, haven't you noticed the recent rise of reddit and SO persona's?
  • 2
    @C0D4 now that you mention it, I think i have.
  • 1
  • 1
    @shoop what is that visual studio for linux? That's a definite no. Think I fucked with it on windows for about 6 hours the other day before I deleted it and looked to see if neovim had a windows version
  • 2
    @M1sf3t it's visual studio code minus the Microsoft, and I don't even know what there is to fuck about in it.

    For the record Visual studio is 100% different from Visual Studio Code, despite having the same name
  • 2
    @M1sf3t array buffers in Javascript can be parsed into strings I think, it's just a sequence of ASCII bytes
  • 2
    @shoop I'll take a look at it. I've never found a need for anything more than a text editor though honestly, I just like using the key bindings in vim better than I do hitting the shift or ctrl keys all the time.

    I think it was the way I pushing it into that array that was messing me up, I could only find 2 examples of how to write an http request in full and they both only went as far as to print them to the console. The one that used the push method printed with process.stdout so i guess that's why I thought they did the same thing, I never noticed the difference at that point because I was still trying to figure out how to make the fetch request wait on the response.
  • 2
    I do the buffer as a string and concatenate on data as well but of course you should know that is insecure because of no length checking. You should design a helper class with length checking which throws an exception or something. Should not take too much time.

    responseReader = new ResponseReader(res);

    try{
    await responseReader.read();
    response.end(responseReader.data)
    }
    catch(ex){
    // Data too long. Might be an attack or faulty response!
    }
  • 1
    @M1sf3t the only thing I miss from code is the git side by side diff. That’s is so well done 👌
  • 1
    @devTea there's a plugin called fugitive that lets you interface with git. Idk how similar it will be but that'll give you the cli gitdiff tool to use inside of the editor.
  • 1
    @AlgoRythm Thanks, I'll keep that in mind.

    How else could you do it, send the whole response back and snatch the body off it client side? That's what I called myself trying to do to begin with but I didn't know to send my response back on close and I still had the "weather !== null" that the sapper docs had used as an example to wait on a db response.

    By the time I figured out that part I had given up on the body and was just tryin to get the data that the http example was logging since I could at least see that it was there intact.
  • 2
    @M1sf3t You can do some magic with pipes / streams if you want to just transfer the data straight from the API to your client with minimal in-between.

    Or check the CORS settings and see if you can cut out the middle man and just have the client directly request the API.
  • 1
    @AlgoRythm I've got my key plugged into an env variable. If I send the request straight from the client then it could be seen in the devtools couldn't it?

    I'm going to look more into letting the data pass through once I figure out how to give this thing props. Sapper has some kind of dynamic routing built in that let's you pass them that way instead of adding them to the body.

    That may be what's different about the "this.fetch" it said to use, it lets me just attach a slug on the end of the route and omit the second argument entirely. Not entirely sure though, I don't have a lot over backend projects lying around for reference 😅

    @alexbrooklyn body-parser came into the picture when it was needed to read the post request on the server. Somehow I got turn around when I was flipping back and forth. Idk what the hell the couple of guys were doing with it on sapper, if I'm understanding this right it doesn't need it for what they were doing.
  • 2
    @M1sf3t I didn't realize you needed a key. Yeah, you need to do at least some of it server-side. Look into streams and piping and all that, should be a little more natural than reading the whole response and then sending it back. Remember that there's response.write, which will send out a chunk of data without closing the connection.
  • 1
    @AlgoRythm ok thanks. I had that in one of the examples in the node docs but i couldn't find anything saying specifically what it was doing at the time. I just found them listed further down in the streaming section though.
  • 1
    @M1sf3t I tried but it just didn’t felt that good
  • 1
    @devTea I'll let you know if I come across another one that looks good. I finally have a reason to be concerned with branching and the like so I've been shopping around for one. Fugitive and git gutter seem to be the most popular so far but there seem to be quite a few others
  • 2
    Oh you have got to be shitting me 🤦‍♂️
  • 0
    Turtles all the way down.
Add Comment