문제없이 node.js에서 다른 프로그램을 실행할 수 있습니다. NODE.js를 통해 MySQL에 연결하려면 터미널에 nodejs mysqlConnection.js
을 입력하고 브라우저에 http://localhost:8080/
을 입력하면 영원히로드됩니다.MySQL에 연결할 때 Nodejs가 페이지를로드 할 수 없습니까?
내 코드는 다음과 같습니다 :
// Include http module,
var http = require('http'),
// And mysql module you've just installed.
mysql = require("mysql");
// Create the connection.
// Data is default to new mysql installation and should be changed according to your configuration.
var connection = mysql.createConnection({
user: "root",
password: "",
database: "framework"
});
// Create the http server.
http.createServer(function (request, response) {
// Attach listener on end event.
request.on('end', function() {
// Query the database.
connection.query('SELECT * FROM words;', function (error, rows, fields) {
response.writeHead(200, {
'Content-Type': 'x-application/json'
});
// Send data as JSON string.
// Rows variable holds the result of the query.
response.end(JSON.stringify(rows));
});
});
// Listen on the 8080 port.
}).listen(8080);
주의 : 나는 문제없이 PHP를 통해 내 데이터베이스에 연결하기 때문에 MySQL의 연결이 올바른지.
NB : 내 안에는 내 js 파일 옆에 node_modules
이라는 폴더가 있는데, 그 안에 mysql 폴더가 있습니다.
나는 NPM을 통해 nodejs를 설치하려고 :
그것은 다운로드 창을 엽니 다 :
그래서이 문제를 해결해야 전부end
핸들러를 제거. 헤더가 인식되지 않는다고 생각합니다. – ALH'application/json'을 사용해보십시오. JSON 데이터에 적합한 콘텐츠 유형입니다. – robertklep
고맙습니다. 그것은 내 문제를 해결! ;-) – ALH