本文介绍了如何将msyql db连接到node.js?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在mysql中有一个数据库,名称是address_book,并有一个karbar表。
i编写此代码进行连接:
我在cmd中的错误是:
i have a database in mysql that name is address_book and have a karbar table.
i write this code for connecting :
and my error in cmd is this :
C:\Users\akbar\Desktop\CODE\node.js\http server>node server.js
Server is running..
{ ConnectionError: Login failed for user 'akbar'.
at Connection.<anonymous> (C:\Users\akbar\Desktop\CODE\node.js\http server\node_modules\mssql\lib\tedious.js:378:25)
at Connection.g (events.js:291:16)
at emitOne (events.js:96:13)
at Connection.emit (events.js:188:7)
at Connection.processLogin7Response (C:\Users\akbar\Desktop\CODE\node.js\http server\node_modules\tedious\lib\connection.js:672:16)
at Connection.message (C:\Users\akbar\Desktop\CODE\node.js\http server\node_modules\tedious\lib\connection.js:1082:21)
at Connection.dispatchEvent (C:\Users\akbar\Desktop\CODE\node.js\http server\node_modules\tedious\lib\connection.js:519:45)
at MessageIO.<anonymous> (C:\Users\akbar\Desktop\CODE\node.js\http server\node_modules\tedious\lib\connection.js:439:23)
at emitNone (events.js:86:13)
at MessageIO.emit (events.js:185:7)
name: 'ConnectionError',
message: 'Login failed for user \'akbar\'.',
code: 'ELOGIN' }
{ ConnectionError: Connection is closed.
at C:\Users\akbar\Desktop\CODE\node.js\http server\node_modules\mssql\lib\main.js:1569:17
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
name: 'ConnectionError',
message: 'Connection is closed.',
code: 'ECONNCLOSED' }
我尝试过:
What I have tried:
var express = require('express');
var app = express();
app.get('/', function (req, res) {
var sql = require("mssql");
// config for your database
var config = {
user: 'akbar',
password: '',
server: 'localhost',
database: 'address_book'
}
// connect to your database
sql.connect(config, function (err) {
if (err) console.log(err);
// create Request object
var request = new sql.Request();
// query to the database and get the records
request.query('select * from dbo.karbar', function (err, recordset) {
if (err) console.log(err)
// send records as a response
res.send(recordset);
});
});
});
var server = app.listen(3000, function () {
console.log('Server is running..');
});
推荐答案
这篇关于如何将msyql db连接到node.js?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!