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

What modules can nodejs routing be implemented through?

青灯夜游
青灯夜游Original
2021-11-30 17:44:432068browse

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 ....')
})

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!

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
Previous article:what is javascript v8Next article:what is javascript v8