arthuro ali
arthuro ali

Reputation: 137

Problems with my routes in my production project with node js ERROR

I am presenting problems with my project in production, I don't know if I have a bad configuration or something like that, I would like to know if you can see something, I will leave you pieces of my code.

In local it works perfectly, it seems that in production I use netlify, it does not take the routes well

This is the file in my routes folder

'use strict'
var express = require('express');
var ArticleController = require('../controllers/article');
var router = express.Router();
var multipart = require('connect-multiparty');
var md_upload = multipart({uploadDir: './upload/articles'});

router.get('/', ArticleController.probar);
router.post('/save', ArticleController.save);
router.get('/articles/:last?', ArticleController.getArticles);
router.get('/article/:id', ArticleController.getArticle);
router.put('/article/:id', ArticleController.update);
router.delete('/article/:id', ArticleController.delete);
router.post('/upload-image/:id?', md_upload , ArticleController.upload);
router.get('/get-image/:image', ArticleController.getImage);
router.get('/search/:search', ArticleController.search);

module.exports = router;

So I go to where I export those paths, which is the app.js file

'use strict'

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var article_routes = require('./routes/article');


app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());

// CORS
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Authorization, X-API-KEY, Origin, X-Requested-With, 
Content-Type, Accept, Access-Control-Allow-Request-Method');
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE');
res.header('Allow', 'GET, POST, OPTIONS, PUT, DELETE');
next();
});

app.use('/api', article_routes);

module.exports = app;

Then I go to my index.js which is where I import the app.js

'use strict'

var mongoose = require('mongoose');
var port = process.env.PORT || 3900;
var app = require('./app');

mongoose.set('useFindAndModify', false);
mongoose.Promise = global.Promise;

mongoose.connect('mongodb+srv://emocrat3:[email protected]/reactProyecto? 
retryWrites=true&w=majority', { useNewUrlParser: true })
    .then(() => {
            console.log('La conexion a mongodb se realizo correctamente!');

            //Crear servidor y ponerme a escuchar peticiones HTTP
            app.listen(port, () => {
                    console.log('Servidor corriendo en http://localhost:' + port);

            });

    });

I will add my repository if you want to see more in depth or I need something to add Github repository

And my website to better observe the error App web The error is presented exactly here ERROR HERE

The only path I have made to hit is

router.get ('/ articles /: last?', ArticleController.getArticles);

And my frontend that I make the query is this

    getArticles = () => {
    axios.get("http://arthuro-gomez-react.netlify.app/api/articles")
        .then(res => {
            this.setState({
                articles: res.data.articles,
                status: 'success'
            });
         });
    }

I hope I have formulated my question well, I would appreciate your time to take a look at my problem. Thank you

Upvotes: 0

Views: 236

Answers (2)

arthuro ali
arthuro ali

Reputation: 137

I already knew what was happening, something very embarrassing haha, I did not take the routes, because my backend was not in line with netlify, only my frontend, so I proceeded to upload my backend project in heroku and only connected my Global variable for the routes, pointing to the link that indicated heroku :) and ready, solved, thank you all for your time.

Basically create a global variable in my frontend that will point to the link where my apis were and now it works :)

Upvotes: 0

Desarrollalab
Desarrollalab

Reputation: 397

router.get ('/ articles /: last?', ArticleController.getArticles);

Try removing the spaces

Because in your router you have

 router.get('/articles/:last?', ArticleController.getArticles);

And that you are doing is

 router.get ('/ articles /: last?', ArticleController.getArticles);

And try removing, because I understan that you will pass any param there is it?

: last?

Upvotes: 1

Related Questions