我正在使用 Node js 和 SQL Server 2008 开发示例应用程序。
要从节点 js 访问 SQL 服务器,请使用 seriate [ http://developer.leankit.com/painless-sql-server-with-nodejs-and-seriate/]。
下面是我的示例代码
文件 ./DBLayer/db.js 代码
var sql = require("seriate");
var config = {
"server": "127.0.0.1",
"user": "sa",
"password": "12kafour~9999",
"database": "Tickets"
};
sql.setDefaultConfig( config );
exports.sqlDb = sql;
并归档 app.js 代码
var express = require('express');
var app = express();
var path = require('path');
var bodyParser = require('body-parser');
var sqlDb = require('./DBLayer/db.js');
//-- set the view engine
app.set('view engine' , 'ejs');
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies ??
extended: true
}));
//-- set the routes
app.get('/login' , function(req , res){
//-- set the view to render
res.render('login' , {title:'Login Screen' , error : ''});
});
app.post('/login' , function(req , res) {
sqlDb.execute( {
query: "SELECT count(*) FROM USERS WHERE LOGIN ='" + req.body.username + "'" + "AND PASSWORD = '" + req.body.pwd + "'"
} ).then( function( results ) {
res.render('login' , {title:'Login Screen' , error : 'u r the one...'});
}, function( err ) {
res.render('login' , {title:'Login Screen' , error : 'Invalid Login/Password.'});
});
});
//-- set the port
var server = app.listen(3000 , function() {
console.log('Server Ready..')
});
当用户输入用户名和密码并提交时,出现“TypeError:sqlDb.execute is not a function”错误。
我不确定为什么会收到此错误。