Nodejs jumps directly to the page

PHPz
Release: 2023-05-25 12:25:37
Original
755 people have browsed it

In website development, the page jump function is an indispensable part. Under normal circumstances, the browser will automatically complete the page jump. When you click a hyperlink or submit a form, the browser will automatically jump to the specified page. But in some special cases, we need to control page jumps on the server side, and Node.js can come in handy at this time.

In Node.js, we can use some modules to control page jumps, such as http, express and other modules. Let us introduce how to use Node.js to implement page jump.

  1. http module implements page jump

When using the http module to implement page jump, we need to use the http.createServer() method to create a server and listen to the client end request. When the client requests, we can use the res.writeHead() method to send the status code and header information to the browser, and then use the res.end() method to send the response body to the browser. The code is as follows:

// 引入http模块
const http = require('http');
 
// 创建服务器
http.createServer(function (req, res) {
   // 发送状态码和头信息
   res.writeHead(302, {
      'Location': 'http://www.baidu.com'
   });
   // 发送响应体
   res.end();
}).listen(3000);
 
console.log('Server running at http://127.0.0.1:3000/');
Copy after login

In the above code, we use the res.writeHead() method to send status code 302 and header information Location to the browser, indicating that we need to redirect to the http://www.baidu.com page. Then use the res.end() method to send an empty response body to the browser, indicating the end of the response. After running the above code, open the browser and visit http://127.0.0.1:3000/, and it will automatically jump to the Baidu page.

  1. express module implements page jump

Another way to implement page jump is to use the express module, which can easily implement routing, middleware and other functions . When using the express module, we need to first install the express module using the npm install express command.

The following is a sample code that uses the express module to implement page jump:

// 引入express模块
const express = require('express');
 
// 创建express实例
const app = express();
 
// 设置路由
app.get('/', function (req, res) {
   // 重定向到百度页面
   res.redirect('http://www.baidu.com');
});
 
// 监听端口
app.listen(3000, function () {
   console.log('Server running at http://127.0.0.1:3000/');
});
Copy after login

In the above code, we use the app.get() method to set the route, indicating that when the browser accesses the root path , you need to redirect to the http://www.baidu.com page. Then use the app.listen() method to listen to the port and output the listening information on the console. After running the above code, open the browser and visit http://127.0.0.1:3000/, and it will automatically jump to the Baidu page.

Summary

Through the above two methods, we can easily implement the page jump function in Node.js. In actual development, we can choose the http module or express module to implement the routing function according to actual needs. In addition, it should be noted that in order to ensure the reliability of page jumps, we should use external URLs for redirection as much as possible, so as to avoid security issues such as cross-site request forgery.

The above is the detailed content of Nodejs jumps directly to the page. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!