Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e507251

Browse files
Added bad and good examples
0 parents commit e507251

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

‎badServer.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Dependencies
2+
var http = require('http'),
3+
fs = require('fs');
4+
5+
// Cache
6+
var cache = {};
7+
8+
// HTTP Server
9+
http.createServer(function (req, res) {
10+
11+
// Parse cookies
12+
var cookie = req.headers.cookie,
13+
cookies = {};
14+
if (cookie) cookie.split(';').forEach(function(item) {
15+
var parts = item.split('=');
16+
cookies[(parts[0]).trim()] = (parts[1] || '').trim();
17+
});
18+
19+
// Logging
20+
var date = new Date().toISOString();
21+
console.log([date, req.method, req.url].join(' '));
22+
23+
// Serve from cache
24+
if (cache[req.url] && req.method === 'GET') {
25+
res.writeHead(200);
26+
res.end(cache[req.url]);
27+
} else {
28+
29+
// Routing
30+
if (req.url === '/') {
31+
if (req.method === '')
32+
response.writeHead(200, {
33+
'Set-Cookie': 'mycookie=test',
34+
'Content-Type': 'text/plain'
35+
});
36+
var ip = req.connection.remoteAddress;
37+
res.write('<h1>Welcome</h1>Your IP: ' + ip);
38+
res.end('<pre>' + JSON.stringify(cookies) + '</pre>');
39+
} else if (req.url === '/person') {
40+
if (req.method === 'GET') {
41+
42+
// Some business logic
43+
fs.readFile('./person.json', function(err, data) {
44+
if (!err) {
45+
var obj = JSON.parse(data);
46+
obj.birth = new Date(obj.birth);
47+
var difference = new Date() - obj.birth;
48+
obj.age = Math.floor(difference / 31536000000);
49+
delete obj.birth;
50+
var data = JSON.stringify(obj);
51+
cache[req.url] = data;
52+
53+
// HTTP reply
54+
res.writeHead(200);
55+
res.end(data);
56+
} else {
57+
res.writeHead(500);
58+
res.end('Read error');
59+
}
60+
});
61+
} else if (req.method === 'POST') {
62+
63+
// Receiving POST data
64+
var body = [];
65+
request.on('data', function(chunk) {
66+
body.push(chunk);
67+
}).on('end', function() {
68+
var data = Buffer.concat(body).toString();
69+
fs.writeFile('./person.json', data, function(err) {
70+
if (!err) {
71+
res.writeHead(200);
72+
res.end('File saved');
73+
} else {
74+
res.writeHead(500);
75+
res.end('Write error');
76+
}
77+
});
78+
});
79+
}
80+
} else {
81+
res.writeHead(404);
82+
res.end('Path not fount');
83+
}
84+
}
85+
86+
}).listen(80);

‎goodServer.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var http = require('http');
2+
3+
var me = { name: 'jura', age: 22 };
4+
5+
var routing = {
6+
'/': 'welcome to homepage',
7+
'/user': me,
8+
'/user/name': function() { return me.name; },
9+
'/user/age': function() { return me.age; }
10+
};
11+
12+
var types = {
13+
object: function(o) { return JSON.stringify(o); },
14+
string: function(s) { return s; },
15+
undefined: function() { return 'not found'; },
16+
function: function(fn, req, res) { return fn(req, res) + ''; },
17+
};
18+
19+
http.createServer(function (req, res) {
20+
var data = routing[req.url],
21+
result = types[typeof(data)](data, req, res);
22+
res.end(result);
23+
}).listen(80);

‎person.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "jura",
3+
"birth": "1990年02月15日"
4+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /