Home > Web Front-end > Front-end Q&A > What modules can nodejs routing be implemented through?

What modules can nodejs routing be implemented through?

青灯夜游
Release: 2021-11-30 17:44:43
Original
2070 people have browsed it

Nodejs routing can be implemented through the url module. The url module is used to process the URL requested by the client. In nodejs, the url address can be parsed through the url module to implement routing operations.

What modules can nodejs routing be implemented through?

The operating environment of this tutorial: windows7 system, nodejs version 12.19.0, DELL G3 computer.

Routing of nodejs

Routing means that we need to have different processing methods for different URLs.

URL module function: process the URL requested by the client

nodejs can parse the url address through the url module and implement routing operations

const http = requrie('http')
const url = requrie('url')
http.createServer((req, res) => {
    //  对请求的路径进行解析
    let pathname = url.parse(req.url).pathname;
    if(pathname == '/'){
        // 相关的处理 
    }else if(pathname == '/login'){
           // 相关的处理
    }....

}).listen(3000, () => {
    console.log('server runnning ....')
})
Copy after login

More node related knowledge , please visit: nodetutorial! !

The above is the detailed content of What modules can nodejs routing be implemented through?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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