node. js before you connect to the database, you need to install the appropriate packages if you need to install SQL Server Node-sqlserver first. We use MySQL as a case to illustrate node. js querying MySQL data.
1, installation Node-mysql
NPM Install Node-mysql
2, through the Express framework to achieve database connectivity
var express = require (' Express '); var mysql = require (' mysql '); var app = Express (); App.use (function (req, res, next) { C Onsole.log ('%s '%s ', Req.method, req.url); Next ();}); var conn = mysql.createconnection ({ host: ' localhost ', User: ' Root ', database: ' Ceshi ', password: ' 123456 ', port:3306}); Conn.connect (); App.get ('/', function (req, res) { conn.query (' SELECT * from Ceshibiao ', function (Err, rows, fields) { if (err) throw err; var data = '; foreach (Rows,function (key,value) { data + = ' <p> ' + ' contents: ' + value.contents + ' </p> '; Data + = '
node. JS Connection Database