質問編集履歴
1
コードを追加します
test
CHANGED
File without changes
|
test
CHANGED
@@ -39,3 +39,133 @@
|
|
39
39
|
at process._tickCallback (internal/process/next_tick.js:180:9)
|
40
40
|
|
41
41
|
[vagrant@localhost nodejs]$ throw err;
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
(settings.js)
|
46
|
+
|
47
|
+
exports.port = 1337;
|
48
|
+
|
49
|
+
exports.host = '192.168.33.10';
|
50
|
+
|
51
|
+
exports.db = 'nodedb';
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
(mongo.js)
|
56
|
+
|
57
|
+
```
|
58
|
+
|
59
|
+
var MongoClient = require('mongodb').MongoClient,
|
60
|
+
|
61
|
+
settings = require('./settings');
|
62
|
+
|
63
|
+
MongoClient.connect("mongodb://localhost/"+settings.db, function(err, db) {
|
64
|
+
|
65
|
+
if (err) { return console.dir(err); }
|
66
|
+
|
67
|
+
console.log("connected to db");
|
68
|
+
|
69
|
+
db.collection("users", function(err, collection) {
|
70
|
+
|
71
|
+
var docs = [
|
72
|
+
|
73
|
+
{name: "taguchi", score: 40},
|
74
|
+
|
75
|
+
{name: "fkoji", score: 80},
|
76
|
+
|
77
|
+
{name: "dotinstall", score: 60}
|
78
|
+
|
79
|
+
];
|
80
|
+
|
81
|
+
collection.insert(docs, function(err, result) {
|
82
|
+
|
83
|
+
console.dir(result);
|
84
|
+
|
85
|
+
});
|
86
|
+
|
87
|
+
});
|
88
|
+
|
89
|
+
});
|
90
|
+
|
91
|
+
コード
|
92
|
+
|
93
|
+
```
|
94
|
+
|
95
|
+
(server.js)
|
96
|
+
|
97
|
+
```
|
98
|
+
|
99
|
+
var http = require('http'),
|
100
|
+
|
101
|
+
fs = require('fs'),
|
102
|
+
|
103
|
+
ejs = require('ejs'),
|
104
|
+
|
105
|
+
qs = require('querystring');
|
106
|
+
|
107
|
+
var settings = require('./settings');
|
108
|
+
|
109
|
+
var server = http.createServer();
|
110
|
+
|
111
|
+
var template = fs.readFileSync(__dirname + '/public_html/bbs.ejs', 'utf-8');
|
112
|
+
|
113
|
+
var posts = [];
|
114
|
+
|
115
|
+
function renderForm(posts, res) {
|
116
|
+
|
117
|
+
var data = ejs.render(template, {
|
118
|
+
|
119
|
+
posts: posts
|
120
|
+
|
121
|
+
});
|
122
|
+
|
123
|
+
res.writeHead(200, {'Content-Type': 'text/html'});
|
124
|
+
|
125
|
+
res.write(data);
|
126
|
+
|
127
|
+
res.end();
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
server.on('request', function(req, res) {
|
132
|
+
|
133
|
+
if (req.method === 'POST') {
|
134
|
+
|
135
|
+
req.data = "";
|
136
|
+
|
137
|
+
req.on("data", function(chunk) {
|
138
|
+
|
139
|
+
req.data += chunk;
|
140
|
+
|
141
|
+
});
|
142
|
+
|
143
|
+
req.on("end", function() {
|
144
|
+
|
145
|
+
var query = qs.parse(req.data);
|
146
|
+
|
147
|
+
posts.push(query.name);
|
148
|
+
|
149
|
+
renderForm(posts, res);
|
150
|
+
|
151
|
+
});
|
152
|
+
|
153
|
+
} else{
|
154
|
+
|
155
|
+
renderForm(posts, res);
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
});
|
160
|
+
|
161
|
+
server.listen(settings.port, settings.host);
|
162
|
+
|
163
|
+
console.log("server listening ...");
|
164
|
+
|
165
|
+
コード
|
166
|
+
|
167
|
+
```
|
168
|
+
|
169
|
+
このようになっています。
|
170
|
+
|
171
|
+
よろしくお願いいたします
|