Nodejs 建立伺服器並與MySQL連線

2022-08-25 10:51:24 字數 814 閱讀 5995

//連線資料庫

const connection =mysql.createconnection();

connection.connect(

function

());

//request 請求資訊 response 返回資訊

let sersql = '資料庫查詢語句';

connection.query(sersql,function

(err,result)

console.log('查詢結果:',result);

response.end(json.stringify(result));//返回值必須是 string 或 buffer 型別

});}

else

if(request.method=="post"))

//到post請求資料發完了之後會執行乙個end事件,這個事件只執行一次

request.on('end', function

() console.log('insert id:',result);

});})

}}).listen(要監聽的埠號);

nodejs入門01 建立http伺服器

安裝好node之後 然後執行 這個程式呼叫了 node.js 提供的http 模組,對所有 http 請求答覆同樣的內容並監聽 3000 埠。開啟瀏覽器訪問會輸出 在開發 node.js 實現的 http 應用時會發現,無論修改了 的哪一部份,都必須終止node.js 再重新執行才會奏效。這是因為 ...

伺服器安裝nodejs

進入 software nodejs node bin目錄,執行.node v命令 ln s software nodejs node bin node usr local bin ln s software nodejs node bin npm usr local bin 修改etc目錄下的pr...

nodejs 配置伺服器

node 是 js 的執行的後台環境,他自身整合了很多模組,整合的模組直接 require 就行了 npm 第三方平台,他也是為 node 服務的,對於 npm 中的模組,先 npm install 然後在使用 node 就是給 js 提供了乙個執行環境,其他任何事情都幹不了 1.建立乙個伺服器 h...