简体   繁体   中英

Expressjs dynamic routing issues

I'm using Express 4.0 and I'm trying to create a routing that show all users from all countries in Homepage.

Switching Country I wish to show all users from that Country.

There are also 2 kind of Users (Teachers and Students) and I wish to do the same in there as well.

On the Teachers page I wish to show all teachers and based on the (previous or not) selected Country I wish to see Teachers from that Country...

I'm having several issues doing this:

1) The Homepage works fine, the homepage with /:countryCode works fine, /teachers and /students do not load anymore... If I put /:countryCode below everything the other pages load... (?!?).

2) Do I also have to create routings like: /:countryCode/teachers ? Or there is a way to store the country code somewhere...?

3) In the Menu as well it seems like I have to create 2 different menu, one normal and one with country code extensions...

At the moment my routing is something like that:

app.get('/', homeController.index);
app.get('/:countryCode', homeController.indexByCountry);
app.get('/teachers', userController.getTeachers);
app.get('/students', userController.getStudents);

I'm using the param :countryCode to query Users from that Country.

There is a better way to create all that? Any Best Practice?

Online I don't see anything similar to this but I think it should be quite popular to have something like that.

I hope you can help.

From what I can figure from your question you're wanting to be able to handle when the country code is explicitly provided and a default case where you want to show all users (students, teachers or both) or use a previously provided country code. Let me know if this is not your intention.

Question 1:

To answer your first question the reason that /teachers and /students will not load is that they are being matched as country codes. My understanding is that express will prioritise your routes in the order they are defined.

Sine /:countryCode is defined above the other two routes the url /teachers will invoke homeController.indexByCountry with the countryCode parameter being equal to "teachers" . By defining these routes above the /:countryCode route then they will have priority and work as intended. So:

app.get('/', homeController.index); // Express will check this first
app.get('/teachers', userController.getTeachers); // Then this
app.get('/students', userController.getStudents); // Then this
app.get('/:countryCode', homeController.indexByCountry); // And finally this

Question 2:

To indicate a new country code for teachers or students one solution could be to define routes like /:countryCode/teachers (or perhaps /teachers/:countryCode which makes more sense to me).

But if you want to save the country code you could use sessions . There is an express middleware package called express-session which will help you do this.

For example:

var express = require('express');
var session = require('express-session');

var app = express();

// Tell the express app to use the session middleware
app.use(session({
  secret: 'app secret key',
  resave: false,
  saveUninitialized: true
}));

app.get('/', function(req, res) {
  if (req.session.countryCode) { // If country code has been set
    res.send('Homepage, country code is currently ' + req.session.countryCode);
  } else {
    res.send('Homepage, country code not set');
  }
});

// Define routes for '/teachers' and '/students' etc.

app.get('/:countryCode', function(req, res) {
  // Set the countryCode property of the session object to the :countryCode
  // variable in the url
  req.session.countryCode = req.params.countryCode;

  res.send("Homepage, country code is now " + req.session.countryCode);
});

app.listen(3000);

Although one problem I encountered was that when I visited the server in my web browser, it would send a GET request to find the favicon and this was being interpreted as a country code and setting it to 'favicon.ico'.

One workaround to this may be checking that the country code provided meets some specific format before setting the session variable, for instance if you may want the country code always to be a 2 character string (Eg. "uk" or "fr").

Question 3:

Since we now know we can remember the country code which was previously set, you can just have a menu that links to /teachers etc. and you will still be able to use the country code from the session variable.

I hope this helps.

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