0

Hi I am pretty new to node & express, and cannot figure out why my get('/',....) is returning undefined and not rendering anything. I am using nunjucks to render and I believe my error should be in the routes file shown below. Any help would really be appreciated!

'use strict'
const express = require('express');
const router = express.Router();
const productCatalog=require('../productCatalog.js');
// could use one line instead: const router = require('express').Router();
const path=require('path');

  // ...
  // route definitions, etc.
  // ...
router.get('/',function(req,res) {
  let maxName=productCatalog.maxSales().name;
  console.log(maxName);
  res.render('index',{ maxProduct: maxName });
});
router.get('/products',function(req,res){
  res.send('Products Page');
});
router.get('/products/:id',function(req,res){
  res.send('Product');
});


// router.get('/stylesheets/style.css',function(req,res){
//   res.sendFile('/stylesheets/style.css');
// });



module.exports = router;

[Edit] (@jfriend00, thanks for the response.) I see the console.log output for maxName, which gives the product with the max sales. the res.send() routes all work, so I figure the issue is with res.render() which is getting called but not rendering anything and logging "GET undefined".

The app.js file with the template engine config is:

'const express = require( 'express' );
const app = express();
const nunjucks = require('nunjucks');
const routes = require('./routes');
const path=require('path');
const productCatalog = require('./productCatalog.js')
var bodyParser = require('body-parser')

app.use( '/', routes );
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

app.set('view engine','html');
app.engine('html',nunjucks.render);
app.use(express.static('acme_products'))
app.use('/',function(req,res,next){
  next();
})


nunjucks.configure('views',{noCache:true});
nunjucks.render('index.html', function (err, output) {
    console.log(output);
});
var port = 3121 || process.env.PORT
app.listen(port,function(){
  console.log('server listening');
})
app.use(function(req,res,next){
  console.log(req.method,req.route)
  next();
})

[Edit] Photo of directories and routes:

Photo of directories and routes

[Edit] Routing Code:

'use strict'
const express = require('express');
const router = express.Router();
const productCatalog=require('../productCatalog.js');
// could use one line instead: const router = require('express').Router();
const path=require('path');

  // ...
  // route definitions, etc.
  // ...
router.get('/',function(req,res) {
  let maxName=productCatalog.maxSales().name;
  console.log(maxName);
  res.render('index',{ maxProduct: maxName });
});
router.get('/products',function(req,res){
  res.send('Products Page');
});
router.get('/products/:id',function(req,res){
    var id=+req.params.id;
    var name=req.params.name;
    var sales=req.params.sales;
    console.log(id);
    var product=productCatalog.find({ id: id });
    console.log(product);
    res.render('product',{productName: name, productSales: sales});
});


router.get('/public/stylesheets/style.css',function(req,res){
  res.sendFile('/public/stylesheets/style.css');
});



module.exports = router;
Rav4
  • 29
  • 7
  • Do your routes get called? Do you see the `console.log()` output for `maxName`? If so, what result do you see? You need to do some basic debugging and share with us what debugging steps you did. If your routes aren't getting called, then you need to also share with us how this route gets used by your main app file. If `res.render()` is getting called, but isn't rendering a page, then you need to also show us how you configure your template engine. Do your routes that just have `res.send()` in them work? – jfriend00 Jul 18 '17 at 01:54
  • Hi @jfriend00, thanks for the response. Yes, I see the console.log output for maxName, which gives the product with the max sales. the res.send() routes all work, so I figure the issue is with res.render() which is getting called but not rendering anything and logging "GET undefined". – Rav4 Jul 18 '17 at 02:39
  • Then show us how you've configured your template engine (which I guess is nunjucks) and explain where the `index` file is located relative to your scripts. – jfriend00 Jul 18 '17 at 02:40
  • The index file is within the repo directory 'acme_products' within a subfolder 'views' – Rav4 Jul 18 '17 at 02:44
  • Here's an example for how you configure nunjucks with Express: https://stackoverflow.com/questions/19912389/how-to-use-html-in-express-framework-with-nunjucks-no-jade and another one that shows the same method: https://github.com/ababra/express-nunjucks/blob/master/app.js, both of which are different than you are doing. – jfriend00 Jul 18 '17 at 02:44
  • Hi, unfortunately that wasnt the fix. I believe it has to do with my path to my CSS file, but I cannot figure out what is wrong since I have defined a static directory. I have attached a photo. Thanks for your help! – Rav4 Jul 18 '17 at 03:24
  • So, is the problem with `index.html` or with your CSS file? If you want help with your CSS file routing, you're going to have to disclose a bunch more information, like what the URL is of the CSS file in your HTML file and where the CSS file is located on your hard drive relative to your main script file and what route you think is going to serve the CSS file before we can even know what the next question to ask you about it is. FYI, screenshots of code are a bad thing. Put code into your question as text and format it appropriately. – jfriend00 Jul 18 '17 at 03:31
  • Duly noted, my apologies. The screenshot was more to show the relative paths to my style.css file but perhaps not the best way to go about it. I defined acme_products as my static and the relative path from there to style should be /public/stylesheets/style.css but the console logs ''no such file'. What am I missing here? – Rav4 Jul 18 '17 at 03:37
  • What's the URL in the web page for the CSS file? And, what does that get translated to when it hits your web server? – jfriend00 Jul 18 '17 at 03:46

0 Answers0