Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialjason limmm
8,009 Pointsnot able to redirect to another route file
here is my code the chrome devtools console responded with a 404 error beacuse i think i was trying to route to a route which is in another if so is there a way i can redirect to a route which is in another file?
const mainroute = require('./routes/index.js');
const products = require('./routes/products.js');
app.use('/', mainroute);
app.use('/products', products);
const express = require('express');
const router = express.Router();
router.get('/', (res, req)=>{
res.render('productslist');
});
module.exports = router;
router.get('/', (req, res)=>{
res.render('mainpage');
document.querySelector('#productbutton').addEventListener('click',()=>{
res.redirect('/products');
});
});
1 Answer
Rohald van Merode
Treehouse StaffHey jason limmm 👋
With only code snippets it's hard to tell for sure why something is or isn't happening. We for example don't have access to the project structure so can't verify that file names and paths are correct. It would therefor be easier to share your code in it's entirety, for example in a workspace 🙂
That being said, the way you've setup your routers seems to be correct at first glance. The only thing that I did notice is that you're trying to use the document
object in the "mainpage" route. You'll want to keep in mind that you're working with server side code here. The document object is part of the Web API, which can only be used in client side code. Node.js doesn't have a document object because it doesn't run in a browser.
Not sure if that is the cause of your issue, but that's the only issue I notice in these snippets 🙂
jason limmm
8,009 Pointsjason limmm
8,009 Pointsis there a way i can import my file into a workspace?
jason limmm
8,009 Pointsjason limmm
8,009 Pointshere is the pug of the mainpage
hope this clarifies something