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 trial

JavaScript

jason limmm
jason limmm
8,004 Points

not 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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

Hey 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
jason limmm
8,004 Points

is there a way i can import my file into a workspace?

jason limmm
jason limmm
8,004 Points

here is the pug of the mainpage

extends layout

block content
    h1 Welcome to apple.con
    button(type='button')#productbutton Press for product

hope this clarifies something