AnaCS
AnaCS

Reputation: 1011

simple get API not working Node express with angular

I have a project with node express and angular, where it seems no matter what I do I cant get the final product to properly make the get request. I have a unfinished project where the request works thought. I am really failing to see what is the difference between the 2 projects.

my server file:

const express = require('express');
const path = require('path');
const app = express();
const routes = require('./server/routes/routes');


app.use(express.static(path.join(__dirname, 'dist')));
app.use('/routes', routes);

app.get('*', (req, res)=>{
    res.sendFile(path.join(__dirname, 'dist/index.html'))
    });

const port = process.env.PORT || 4600;

app.listen(port, (req, res)=>{
console.log(`RUNNING on port ${port}`);
});

The console log also doesn't happen since the request results in an error

enter image description here

my route.js file:

const express = require('express');
const router = express.Router();
const axious = require('axios');
const PostAPI = 'https://jsonplaceholder.typicode.com';


router.get('/', (req, res) => {
    axious.get(`${PostAPI}/posts`).then(posts => {
        console.log(posts.data);

         res.status(200).json(posts.data);


     }).catch(error => {
     res.status(500).send(error);
     })
});
module.exports = router;

I am not getting any error on running the application or any path errors. But I am also never getting the result of the API anymore... I am not sure what is wrong here. A replica of my entire project is here on this github, so you can see the angular part of the project.

https://github.com/ana-acoss/Node-Express-and-Angular- I am getting this only when I run my app enter image description here

I have been trying and trying to find out what is the problem but still nothing.

Upvotes: 1

Views: 1004

Answers (1)

josh.trow
josh.trow

Reputation: 4901

Your config is mapping a response handler to /routes, but then you are requesting /posts? I'm thinking you just need to change routes to posts and then you should hit your backend call.

app.use('/routes', routes);

app.get('*', (req, res)=>{
    res.sendFile(path.join(__dirname, 'dist/index.html'))
    });

Upvotes: 2

Related Questions