Home >Web Front-end >Front-end Q&A >nodejs page jump parameter transfer

nodejs page jump parameter transfer

PHPz
PHPzOriginal
2023-05-18 15:10:38726browse

Node.js is a lightweight JavaScript runtime environment that allows a JavaScript interpreter to run code on the server. As a result, Node.js has become a popular server-side development language commonly used for building web applications.

In Node.js, you can easily build web applications using the Express framework. In web applications, it is often necessary to jump to pages and pass parameters in order to display different page content. This article will introduce how to implement page jump and pass parameters in Express.

  1. Page jump in Express

In Express, page jump is implemented through the res.redirect() function. The res.redirect() function accepts a URL string as a parameter and redirects the browser to the page specified by the URL. For example:

app.get('/home', function(req, res) {
  res.redirect('/login');
});

In the above code, when the user accesses the "/home" path, the server will jump the user to the "/login" path.

  1. Passing parameters in Express

If we need to pass parameters, we can use URL query parameters or route parameters. URL query parameters are key/value pairs located at the end of the URL, such as "/user?name=john&age=30". Routing parameters are in the path, such as "/user/:name".

2.1 URL query parameters

In Express, we can use the req.query object to obtain URL query parameters. For example:

app.get('/user', function(req, res) {
  var name = req.query.name;
  var age = req.query.age;
  res.send('Name: ' + name + ', Age: ' + age);
});

In the above code, when the user accesses the "/user?name=john&age=30" path, the server will return "Name: john, Age: 30".

2.2 Routing parameters

In Express, we can use routing parameters to dynamically pass parameters. For example:

app.get('/user/:name', function(req, res) {
  var name = req.params.name;
  res.send('Name: ' + name);
});

In the above code, when the user accesses the "/user/john" path, the server will return "Name: john".

  1. Implement page jump and pass parameters at the same time in Express

In Express, we can achieve page jump at the same time by passing query parameters or routing parameters in the URL Turn and pass parameters. For example:

app.get('/login', function(req, res) {
  var name = req.query.name;
  var age = req.query.age;
  res.render('login', { name: name, age: age });
});

app.post('/login', function(req, res) {
  var name = req.body.name;
  var age = req.body.age;
  res.redirect('/user/' + name + '?age=' + age);
});

app.get('/user/:name', function(req, res) {
  var name = req.params.name;
  var age = req.query.age;
  res.render('user', { name: name, age: age });
});

In the above code, when the user accesses the "/login" path, the server will present a form to the user to enter their name and age. When the user submits the form, the server will redirect to the "/user/:name" path and pass the query parameter "age" in the URL. Finally, the server will render a page with the username and age.

Summary

In this article, we introduced how to implement page jump and pass parameters in Express. We can use the res.redirect() function to jump to the page, and we can use URL query parameters or routing parameters to pass parameters. When implementing page jumps and passing parameters at the same time, we can pass query parameters or routing parameters in the URL.

The above is the detailed content of nodejs page jump parameter transfer. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn