Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
157
rated 0 times [  159] [ 2]  / answers: 1 / hits: 7856  / 2 Years ago, wed, may 11, 2022, 12:00:00

I'm building React/Redux/Node app that incorporates Spotify's API with authorization code flow. It's a SPA with React-Router that has three routes: Login, Timeline, and Results. When traversing from Timeline to Results, I get this error:


POST http://localhost:3001/login net::ERR_CONNECTION_REFUSED

Here is the Github repo.


Here is a video of the error with Chrome DevTools open.


useAuth.jsx (custom hook)


import { useState, useEffect } from 'react'
import axios from 'axios'

const useAuth = (code) => {

const [accessToken, setAccessToken] = useState()

useEffect(() => {
axios.post('http://localhost:3001/login', {
code,
}).then(res => {
setAccessToken(res.data.accessToken)
window.history.pushState({}, null, '/')
}).catch((err) => {
console.log(err)
})
}, [code])

return accessToken
}
export default useAuth

server.js


require('dotenv').config()
const express = require('express');
const SpotifyWebApi = require('spotify-web-api-node');
const cors = require('cors')
const bodyParser = require('body-parser')

const app = express();
app.use(cors())
app.use(bodyParser.json())

app.post('/login', (req, res) => {
const code = req.body.code
const spotifyApi = new SpotifyWebApi({
redirectUri: process.env.REDIRECT_URI,
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
})

spotifyApi.authorizationCodeGrant(code).then(data => {
res.json({
accessToken: data.body.access_token,
refreshToken: data.body.refresh_token,
expiresIn: data.body.expires_in,
})
})
.catch(() => {
res.sendStatus(400)
})
})

app.listen(3001)

App.js


import {useState, useEffect} from 'react'
import { Login } from './Login'
import { Timeline } from './Timeline'
import { Results } from './Results'
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom'
import './App.css'

const code = new URLSearchParams(window.location.search).get('code');

const App = () => {

const [composers, setComposers] = useState([]);

useEffect(() => {
const fetchComposers = async () => {
const response = await fetch('composers.json');
const data = await response.json();
const listofComposers = data.composers.map((composer) => composer)
setComposers(listofComposers);
}
fetchComposers();
}, []);

return (

<Router>
<Routes>
<Route path='/' element={<Login code={code} />} />
<Route path='/timeline' element={<Timeline composerData={composers} />} />
<Route path='/results' element={<Results code={code} />} />
</Routes>
</Router>

);
}

export default App;

Results.jsx


export const Results = ({ code }) => {
const accessToken = useAuth(code)
console.log(accessToken)

When I console.log(accessToken), it returns undefined.


More From » node.js

 Answers
7

Connection Refused means your app isn't listening on port 3001, on localhost.
Chrome is trying to connect to it, but can't.


(A TCP SYN packet is sent and met with a TCP RST, but this is not relevant to your question)


try changing your app.listen to this and see what happens:


server.listen(3001, 'localhost'); // or server.listen(3001, '0.0.0.0'); for all interfaces
server.on('listening', function() {
console.log('Express server started on port %s at %s', server.address().port, server.address().address);
});

also, a bit of a funny question:
are you sure you ran your app?
specifically the js file server.js...


because if it isn't running it most certainly can't be listening on any port, let alone 3001.


verify it's running and listening on the correct port using TCPView or Get-NetTCPConnection in powershell


[#149] Sunday, April 24, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
wilson

Total Points: 27
Total Questions: 93
Total Answers: 93

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
wilson questions
Tue, Aug 9, 22, 00:00, 2 Years ago
Wed, May 20, 20, 00:00, 4 Years ago
Wed, May 13, 20, 00:00, 4 Years ago
;