0

Please tell me how should I do server.js

app.get('/playlist', (req, res) => {
    fetch("http://www.bbc.co.uk/radio1/playlist.json")
        .then((res) => res.json())
        .then((data) => {
            res.send(data);
        })
        .catch((err) => {
            res.writeHead(500);
            res.end("Failed to load data: " + err);
        });
})

React app.js:

componentDidMount() {
    axios.get('/playlist').then(function(response){
     alert(response)
   }).catch(function(error){
     alert(error)
   });
}
Mayank Shukla
  • 100,735
  • 18
  • 158
  • 142
mahi
  • 19
  • 1
  • 8
  • Are your server and client apart of the same project? Try putting the full url in axios.get: axios.get('http:// localhost/playlist') – mcjcloud Dec 01 '17 at 06:44
  • Yes in a same project. but client runs on http://localhost:8080/ and server runs on http://localhost:8181/ so how to give the URL please tell me. – mahi Dec 01 '17 at 06:47
  • What happens when you put localhost:8181/playlist? Same error? – mcjcloud Dec 01 '17 at 06:48
  • GET http://localhost:8181/playlist net::ERR_CONNECTION_REFUSED . It is giving Like that error . please tell me how to resolve this. – mahi Dec 01 '17 at 06:50
  • Not sure if this will help, but it looks like you don't have a semicolon at the end of your app.get block. If you're using ES6 syntax that shouldn't be a problem though. – mcjcloud Dec 01 '17 at 06:55
  • now that problem is resolve but it is not fetching actual output from nodejs. App
    ..... Now it is giving like that response. But I dont want this
    – mahi Dec 01 '17 at 07:19
  • Put console.log statements in your nodejs to see what's being executed. If your catch block is being executed then you can console.log(err) to see what it is. – mcjcloud Dec 01 '17 at 07:21
  • Seems like a cross origin error. The Ajax requests to localhost:8181 but the origin is localhost:8080, not the same origin. – Shu Ding Dec 01 '17 at 10:29
  • Try these solutions: https://stackoverflow.com/questions/7067966/how-to-allow-cors – Shu Ding Dec 01 '17 at 10:31

0 Answers0