Node.js 정적 요청(8.9)

서버 생 성
const http = require('http');
const ip = '192.168.169.129';
const port = 3000;

var f = function(req,res){
res.writeHead(200,{'Content-Type':'text/plain'});
res.write("Hello !");
res.end();
}

var f2 = function(){
console.log('server start');
}

var server = http.createServer(f);
server.listen(port,ip,f2);

URL 내용 추가
const http = require('http');
const ip = '192.168.169.129';
const port = 3000;
const url = require('url');

var f = function(req,res){
  var path = url.parse(req.url).pathname;
  res.write(path);
  res.end();
}

var f2 = function(){
  console.log('server start');
}

var server = http.createServer(f);
server.listen(port,ip,f2);

파일 읽 기
const http = require('http');
const ip = '192.168.169.129';
const port = 3000;
const fs = require('fs');

var f3 = function(err,data){
  console.log(data.toString());
}

fs.readFile('q.index.html',f3);

var f = function(req,res){
  res.writeHead(200,{'Content-Type':'text/html'})
  res.write("Hello Kitty !");
  res.end();
}

var f2 = function(){
  console.log('server start');
}

var server = http.createServer(f);
server.listen(port,ip,f2);

브 라 우 저 출력 파일 내용
const http = require('http');
const ip = '192.168.169.129';
const port = 3000;
const url = require('url');
const fs = require('fs');

var data = fs.readFileSync('./index.html');

var f = function(req,res){
  var path = url.parse(req.url).pathname;
  res.write(path);
  res.write(data.toString());
  res.end();
}

var f2 = function(){
  console.log('server start');
}

var server = http.createServer(f);
server.listen(port,ip,f2);

좋은 웹페이지 즐겨찾기