druv5319 / Sneaks-API

A StockX, FlightClub, Goat, and Stadium Goods API all in one. This sneaker API allows users to search sneakers and track and compare prices while providing additional info such as product links and images
438 stars 118 forks source link

Integrating with Express or React application #7

Closed SyedHusaini closed 3 years ago

SyedHusaini commented 3 years ago

I tried integrating the API with an existing MERN Stack project I am working on. Initially I wanted to make a direct call to the API from the front end (built in React), and after installing the package, I included the following code in my component:

componentDidMount() {
        const sneaksAPI = require('sneaks-api');
        const sneaks = new sneaksAPI();

        sneaks.getProductPrices("FY2903", function (err, product) {
            console.log(product)
        })
    }

This resulted in the following error:

Failed to compile.
./node_modules/http2-wrapper/source/index.js  
Module not found: Can't resolve 'http2' in '<project-directory>\node_modules\http2-wrapper\source'

I then tried to integrate it with my back end (built with express) where after installation, I included the following code in one of my controller functions:

const sneaksAPI = require('sneaks-api');
        const sneaks = new sneaksAPI();

        sneaks.getProductPrices("FY2903", function (err, product) {
            console.log(product)
        })

Running this code resulted in this an error on the first line:

NEW API
UNCAUGHT EXCEPTION! �💥 Shutting down.
Error listen EADDRINUSE: address already in use :::3080
Error: listen EADDRINUSE: address already in 
use :::3080
    at Server.setupListenHandle [as _listen2] (net.js:1300:14)
    at listenInCluster (net.js:1348:12)      
    at Server.listen (net.js:1436:7)
    at Function.listen (<project-directory>\..\node_modules\express\lib\application.js:618:24)
    at Object.<anonymous> (<project-directory>\..\node_modules\sneaks-api\index.js:18:5)
    at Module._compile (internal/modules/cjs/loader.js:956:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Module.require (internal/modules/cjs/loader.js:849:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at exports.getSneakers (<project-directory>\controllers\sneakerController.js:115:21)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

How do I integrate the API in my project?

druv5319 commented 3 years ago

I've tried to install my API on a react server as well but for some reason it's not compatible with http2 so I ended up setting up a local server and used the react server to send GET requests to the API server.

As for your second issue, I don't believe this has anything to do with the Sneaks API. I just tried running a new node.js project with the sneaks-api package installed and its working fine. It seems that your project is trying to connect to localhost:3080 but cant for some reason because its already in use.

I found a couple of links that might help your issue:

https://github.com/visionmedia/supertest/issues/568 https://medium.com/@BarakaMahili/getting-freaked-out-by-node-eaddrinuse-address-already-in-use-error-6b2ed92fd975

Let me know if they help you out.