NodeJ S-urlモジュール

1596 ワード

  • モジュール作成サービスを導入する
  • var http = require('http');
    var url = require('url');
    var server = http.createServer();
    
  • レスポンス
  • 処理
    server.on('request',function(req,res){
    
        //req.url :    
        //console.log(req.url);///a/b/1.html
        //  URL
       var  urlStr = url.parse(req.url);
        console.log(urlStr);
        //Url {
        //protocol: null,
        //    slashes: null,
        //    auth: null,
        //    host: null,
        //    port: null,
        //    hostname: null,
        //    hash: null,
        //    search: '?a=1',
        //    query: 'a=1',
        //    pathname: '/a/b/1.html',
        //    path: '/a/b/1.html?a=1',
        //    href: '/a/b/1.html?a=1' }
        switch (urlStr.pathname)
        {
            case '/':
                //  
                res.writeHead(200,{
                    'content-type':'text/html;charset=utf-8',
                })
                res.end('

    '); break; case '/user': res.writeHead(200,{ 'content-type':'text/html;charset=utf-8', }) res.end('

    '); break; default : // res.writeHead(404,{ 'content-type':'text/html;charset=utf-8', }) res.end('

    '); break; break; } });

  • 傍受
  • server.listen('8080','localhost');