简体   繁体   中英

Express routing issue

I am learning expressjs and I've been stuck at moment how to make the navigation between pages:

What I've done:
1. Installed express, and converted regular html to jade format.
2. In app.js I've added following code:

app.get('/', function(req, res){
  res.render('views/index.jade', { title: 'index' });
});

app.get('/about', function(req, res){
  res.render('views/portfolio.jade', { title: 'about' });
});
  1. All files I've stored in views folder and in index.jade I've added following code:

      a.selected(href='/views/index.jade') TIMELINE a(href='/views/portfolio.jade') PORTFOLIO a(href='/views/about_me.jade') ABOUT ME a(href='/views/store.jade') STORE 
  2. When I click on portfolio button, the following error appears: 在此处输入图片说明

You need to link href the route path:

a.selected(href='/') TIMELINE
      a(href='/about') PORTFOLIO
      a(href='/about') ABOUT ME
      a(href='/store') STORE

You are actually creating routes, which enable a browser to ask the server for data on a specific path. What the server sends to the browser based on that path is up to the programmer.

In your case, you're internally configuring the /about route to render the file views/portfolio.jade .

Thus, instead of linking to the .jade files like you're doing, you should be linking to the actual routes you created:

  a.selected(href='/') TIMELINE
  a(href='/portfolio') PORTFOLIO
  a(href='/about') ABOUT ME
  a(href='/store') STORE

Assuming you have the following routes:

app.get('/', function(req, res){
  res.render('views/index.jade', { title: 'index' });
});

app.get('/about', function(req, res){
  res.render('views/about_me.jade', { title: 'about' });
});

app.get('/store', function(req, res){
  res.render('views/store.jade', { title: 'store' });
});

app.get('/portfolio', function(req, res){
  res.render('views/portfolio.jade', { title: 'portfolio' });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM