정현희

Node.js 실습

"use strict"
var http = require('http');
var server = http.createServer(function(req, res) {
res.writeHeader(200, {"Content-Type": "text/plain"});
res.write("I changed port 3000 to 2500! I don't have any problem hahaha");
res.end();
});
server.listen(2500, function() {
console.log("Sever listeining on http://localhost:2500");
});
"use strict"
var http = require('http'),
path = require('path'),
url = require('url'),
fs = require('fs');
var DOCUMENT_ROOT = "../Quiz3/";
var server = http.createServer(function(req, res)
{
var reqPath = url.parse(req.url).pathname;
if(reqPath == "/")
{
reqPath = "Quiz3.html";
}
var fullPath = path.join(process.cwd(), DOCUMENT_ROOT, reqPath);
fs.readFile(fullPath, "binary", function(err, file)
{
if(err)
{
if(err.code == "ENOENT")
{
console.log("SEND 404 for " + req.url);
res.writeHeader(404, {"content-type" : "text/html"});
res.write("<h1>Not found(/h1)");
res.end();
}
else
{
console.error("Error", err);
res.writeHeader(500, {"content-type" : "text/plain"});
res.write(err + "\n");
res.end();
}
}
else
{
console.log("SEND 200 for" + req.url);
res.writeHeader(200);
res.write(file, "binary");
res.end();
}
});
});
server.listen(3000, function()
{
console.log("Server listeining on http://localhost:3000");
});