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;