Saturday 6 May 2017

Trouble with express routing

Hello I am fairly new to node, I having trouble with routing in an express app. I have looked at documentation and searched for answers, tried a little bebugging but no luck. here is current code on github https://github.com/Ongomobile/my-api

I am using express router to route from 2 files in my routes directory, the index route works fine but the end points in routes/products.js do not work with app.use(), they do work if I use app.get() or if I put end points in routes/index.js

In my server.js file (I believe my problem is in code below)

var indexRoutes   = require('./routes/index'),
productRoute = require(‘./routes/products’)

app.use('/', indexRoutes);
app.use('/product', productRoute);

Routes/products.js
var express = require('express');
var router = express.Router();
var Product = require('../model/product');
router.get('/product', function(req, res) {
    Product.find({}, function(err, products) {

        if(err) {
            res.status(500).send({error: "Products not found..."});
        }else {
            // res.send(products);
            res.send("<h1>Products will show here when populated</h1>");
        }
    });
});

router.post('/product', function(request, response) {
 …….
  });

module.exports = router;



via Mike Haslam

No comments:

Post a Comment