ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • node.js 끄적
    카테고리 없음 2020. 6. 20. 07:02

    node.js 웹서버 구동

    var http = require('http');
    var fs = require('fs');
    var app = http.createServer(function (request, response) {
        var url = request.url;
        if (request.url == '/') {
            url = '/index.html';
        }
        if (request.url == '/favicon.ico') {
            response.writeHead(404);
            response.end();
            return;
        }
        response.writeHead(200);
        response.end(fs.readFileSync(__dirname + url));
    
    });
    app.listen(3000);

    url로 입력된 값 사용

    var http = require('http');
    var fs = require('fs');
    var url = require('url');
    
    var app = http.createServer(function (request, response) {
        var _url = request.url;
        var queryData = url.parse(_url, true).query;
        console.log(queryData.id); // localhost:300/?ID=HTML에서 HTML
        if (_url == '/') {
            _url = '/index.html';
        }
        if (_url == '/favicon.ico') {
            return response.writeHead(404);
        }
        response.writeHead(200);
        response.end(queryData.id);
    
    });
    app.listen(3000);

    ?id= 에 따른 결과

    $(queryData.id)

    루트로 접속할때

    if (_url == '/') { // home root
            title = 'Welcome';
        }

     

    댓글

© 2018 TISTORY. All rights reserved.