| 1234567891011121314151617181920212223242526272829303132 |
- //var url = require('url');
- //var adr = 'http://localhost:1337/index.html?student=홍길동&classname=nodejs';
- //var q = url.parse(adr, true);
- //
- //console.log(q.host); // returns 'localhost:1337'
- //console.log(q.pathname); // returns '/index.html'
- //console.log(q.search); // returns '?student=홍길동&classname=nodejs'
- //
- //var qdata = q.query; // returns an object: { student: '홍길동', classname: 'nodejs' }
- //console.log(qdata.classname); // returns 'february'
-
-
- var http = require('http');
- var url = require('url');
- var fs = require('fs');
-
- http.createServer(function (req, res) {
- var q = url.parse(req.url, true);
- // '.'은 현재 디렉토리를 의미하며, ./nodejs.html은 현재 디렉토리의 nodejs.html파일
- var filename = "." + q.pathname;
- console.log(filename);
- fs.readFile(filename, function(err, data) {
- if (err) {
- res.writeHead(404, {'Content-Type': 'text/html'});
- return res.end("404 Not Found");
- }
- res.writeHead(200, {'Content-Type': 'text/html'});
- res.write(data);
- return res.end();
- });
- }).listen(1337); // listen(1337, '127.0.0.1') 와 동일
- console.log('Server running at http://127.0.0.1:1337/');
|