1

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.

1 Answer 1

2

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'))
    });
Sign up to request clarification or add additional context in comments.

1 Comment

It was this! Thank you so much, I can't believe I just didn't see it...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.