回答編集履歴
1
expressで試す
test
CHANGED
@@ -75,3 +75,85 @@
|
|
75
75
|
```
|
76
76
|
|
77
77
|
の直後に`res.render('index', { login: req.session.login, posts});`を呼ぶべきです
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
追記
|
82
|
+
|
83
|
+
```JavaScript
|
84
|
+
|
85
|
+
// express
|
86
|
+
|
87
|
+
const express = require('express');
|
88
|
+
|
89
|
+
const app = express();
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
// mysql
|
94
|
+
|
95
|
+
const mysql = require('mysql');
|
96
|
+
|
97
|
+
const connection = mysql.createConnection({
|
98
|
+
|
99
|
+
host : '192.168.33.11',
|
100
|
+
|
101
|
+
user : 'hoge',
|
102
|
+
|
103
|
+
password : 'hoge'
|
104
|
+
|
105
|
+
});
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
// 結果
|
110
|
+
|
111
|
+
let posts = [];
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
var server = app.listen(3000, function(){
|
116
|
+
|
117
|
+
console.log("Node.js is listening to PORT:" + server.address().port);
|
118
|
+
|
119
|
+
});
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
app.get('/', function(req, res, next) {
|
124
|
+
|
125
|
+
connection.query(`
|
126
|
+
|
127
|
+
SELECT *
|
128
|
+
|
129
|
+
FROM \`master\`.\`hoge\`
|
130
|
+
|
131
|
+
WHERE \`id\` = ?;
|
132
|
+
|
133
|
+
`, 1072353, function (error, results, fields) {
|
134
|
+
|
135
|
+
if(error) {
|
136
|
+
|
137
|
+
console.log(error);
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
else {
|
142
|
+
|
143
|
+
posts = results;
|
144
|
+
|
145
|
+
console.log(results);
|
146
|
+
|
147
|
+
res.json(results);
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
);
|
154
|
+
|
155
|
+
});
|
156
|
+
|
157
|
+
```
|
158
|
+
|
159
|
+
`connection.connect()`と`connection.end()`を入れると複数回アクセスでエラー出るので処理をなくすと動くのは確認しました
|