host multiple pages on nodejs

前端 未结 2 1491
独厮守ぢ
独厮守ぢ 2021-02-06 03:47

So I have my app at http://localhost:8080/

How can I have http://localhost:8080/subpage ? Because it seems like any page that hits :8080 pulls

相关标签:
2条回答
  • 2021-02-06 04:40

    Here is a start:

    var http=require('http');
    var url=require('url');
    
    var server=http.createServer(function(req,res){
        var pathname=url.parse(req.url).pathname;
        switch(pathname){
            case '/subpage':
                res.end('subpage');
            break;
            default:
                res.end('default');
            break;
        }
    
    }).listen(8080);
    
    0 讨论(0)
  • 2021-02-06 04:48

    I hit the same problem as you did, and I think what we were both looking for is basically a routing engine for node.js. Basically, okay fine I get the hello-world example for nodejs, but how do I build something that responds to different requests?

    For future users who land on this page via google, you must look at Express.js and this excellent guide and intro into express, Understanding Express.js. These two will solve the problem

    0 讨论(0)
提交回复
热议问题