수

Node.js 쿼리 문자열 읽기 , 쿼리 문자열 분할하기

· 2019-01-10 (목) 22:15:22 · 2589

쿼리 문자열 읽기
http.createServer()에 req 객체는 클라이언트로부터 요구를 나타내는 인수입니다.
이 객체에는url이라는 속성이 있습니다.
이 속성은 도메인 이름 뒤에 오는 URL의 일부를 포함합니다.

demo_http_url.js
var http=require("http");
http.createServer(function(req, res){
    res.writeHead(200, {"Content-Type", "text/html"});
    res.write(req.url);
    res.end()
}).listen(8080);

http://localhost:8080/summer 를 실행하면 화면에 출력은

/summer 가 출력되게 된다.


쿼리 문자열 분할
쿼리 문자열을 URL모듈과 같이 읽을 수 있는 부분으로 쉽게 나눌 수 있는 내장모듈

예)
쿼리 문자열을 읽을 수 있는 부분으로 나눕니다.

var http= require("http");
var url = require("url");

http.createServer(function(req, res){
    res.writeHead(200, {"Content-Type", "text/html"});
    var q = url.parse(req.url, true).query;
    var text = q.year + " " + q.month;
    res.end(txt)
}).listen(8080);

http://localhost:8080/?year=2017&month=July

화면 출력
2017 July

|
댓글을 작성하시려면 로그인이 필요합니다.

개발자팁

5,403건

개발과 관련된 유용한 정보를 공유하세요. 질문은 QA에서 해주시기 바랍니다.

+
분류 제목 글쓴이 날짜 조회
PHP 19-06-24 조회 3,192
PHP 19-06-09 조회 3,812
MySQL 19-04-01 조회 4,867
jQuery 19-03-20 조회 4,252
node.js 19-02-12 조회 3,353
node.js 19-02-12 조회 3,088
node.js 19-02-12 조회 3,352
node.js 19-02-07 조회 3,327
node.js 19-02-07 조회 3,065
node.js 19-02-07 조회 3,211
node.js 19-02-04 조회 2,950
node.js 19-02-04 조회 2,927
node.js 19-02-04 조회 3,370
node.js 19-01-31 조회 2,479
node.js 19-01-31 조회 2,629
node.js 19-01-31 조회 3,107
node.js 19-01-28 조회 2,870
node.js 19-01-28 조회 2,952
node.js 19-01-28 조회 2,793
node.js 19-01-22 조회 3,124
node.js 19-01-22 조회 2,886
node.js 19-01-22 조회 3,310
node.js 19-01-16 조회 2,755
node.js
[node.js]
19-01-16 조회 2,650
node.js 19-01-16 조회 9,327
node.js 19-01-12 조회 4,404
node.js 19-01-12 조회 3,006
node.js 19-01-12 조회 3,152
node.js 19-01-11 조회 2,696
node.js 19-01-11 조회 4,052