정현희

Node.js 실습

1 +"use strict"
2 +
3 +var http = require('http');
4 +var server = http.createServer(function(req, res) {
5 + res.writeHeader(200, {"Content-Type": "text/plain"});
6 + res.write("I changed port 3000 to 2500! I don't have any problem hahaha");
7 + res.end();
8 +});
9 +
10 +server.listen(2500, function() {
11 + console.log("Sever listeining on http://localhost:2500");
12 +});
1 +"use strict"
2 +
3 +var http = require('http'),
4 + path = require('path'),
5 + url = require('url'),
6 + fs = require('fs');
7 +
8 +var DOCUMENT_ROOT = "../Quiz3/";
9 +var server = http.createServer(function(req, res)
10 +{
11 + var reqPath = url.parse(req.url).pathname;
12 + if(reqPath == "/")
13 + {
14 + reqPath = "Quiz3.html";
15 + }
16 + var fullPath = path.join(process.cwd(), DOCUMENT_ROOT, reqPath);
17 + fs.readFile(fullPath, "binary", function(err, file)
18 + {
19 + if(err)
20 + {
21 + if(err.code == "ENOENT")
22 + {
23 + console.log("SEND 404 for " + req.url);
24 + res.writeHeader(404, {"content-type" : "text/html"});
25 + res.write("<h1>Not found(/h1)");
26 + res.end();
27 + }
28 + else
29 + {
30 + console.error("Error", err);
31 + res.writeHeader(500, {"content-type" : "text/plain"});
32 + res.write(err + "\n");
33 + res.end();
34 + }
35 + }
36 +
37 + else
38 + {
39 + console.log("SEND 200 for" + req.url);
40 + res.writeHeader(200);
41 + res.write(file, "binary");
42 + res.end();
43 + }
44 + });
45 +});
46 +
47 +server.listen(3000, function()
48 +{
49 + console.log("Server listeining on http://localhost:3000");
50 +});