2016-10-22 5 views
1

私の会社はSQL Serverを使用しています。レポートダッシュボードを開発したいので、Nodejs + Angularjsを使用して開発したいと考えています。Angularjsで作成されたWebサイトにデータを表示する方法、SQL ServerからNode.jsに取得されたデータ

私は以下のコードを持っています。あなたは私

  1. サーバー側のプログラムのためにAngularjs
  2. 使用のNode.jsを使ってHTMLページ内のSQL Serverの
  3. 表示からデータを取得する小さな例を与えることができます。

コードがあります。それは動作していません。

//server.js 
var express = require('express'); 
var app = express(); 

app.use(express.static(__dirname + '/website')); 

require('./app/routes.js')(app); 

var server = app.listen(5000, function() { 
    console.log('Server is running..'); 
}); 


//router.js 
module.exports = function(app) { 

// Load index file 

app.get('*', function(req, res) { 

    res.sendfile('./index.html'); // load the index from static files directory. 

    var sql = require("mssql"); 

    // config for your database 
     var config = { 
     user: 'sa', 
     password: 'sqlserver', 
     server: 'FKHADER01', 
     database: "master" 
      }; 

    // 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 count(*) CT from sales', function (err, recordset) { 


      if (err) console.log(err); 

      // send records as a response 
      res.send(recordset); 
      var count = recordset[0].CT; 

     }); 
    }); 



});}; 



//index.html 
<!DOCTYPE html> 
<html> 
<script  src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"> </script> 
<body> 

<div ng-app="" ng-init="count='5'"> 

<p>The name is <span ng-bind="count"></span></p> 

</div> 

</body> 
    </html> 

あなたは

答えて

0
I understand you are able to get the data from Sql server in your server.js Page. You would now want to send the data to the views. 
So what you can do is use a view engine like EJS (npm install ejs) 
and app.set('view engine', 'ejs'); 
you can pass the data which you have in the "recordset" variable to 
the EJS page. The rendering would look something like 
res.render('./index.ejs',message:recordset , count :count) 

You would also need to create an index.ejs file and copy all the 
context of index.html to it. The .ejs can simply be created 
by renaming index.html to index.ejs 

In the index.ejs , inside script tags ,you can retrieve the 
recordset & count as 
<script>var rawQuery = <%message%>; var count = <%count%></script> 

You can then apply angular to manipulate the data in rawQuery,count 
and attach it to the scope in the required controller. 


var app = angular.module('myApp', []); 
app.controller('myCtrl', function($scope) { 
$scope.count = count; 
}); 

Hope this link helps. 
https://scotch.io/tutorials/use-ejs-to-template-your-node-application 
I understand you are able to get the data from Sql server in your server.js Page. You would now want to send the data to the views. So what you can do is use a view engine like EJS (npm install ejs) and app.set('view engine', 'ejs'); you can pass the data which you have in the "recordset" variable to the EJS page. The rendering would look something like 

res.render('./index.ejs',message:recordset , count :count) 

You would also need to create an index.ejs file and copy all the context of index.html to it. The .ejs can simply be created by renaming index.html to index.ejs 

In the index.ejs , inside script tags ,you can retrieve the recordset & count as 

<script>var rawQuery = <%message%>; var count = <%count%></script> 

You can then apply angular to manipulate the data in rawQuery,count and attach it to the scope in the required controller. 

var app = angular.module('myApp', []); 
app.controller('myCtrl', function($scope) { 
$scope.count = count; 
}); 
Hope this link helps. https://scotch.io/tutorials/use-ejs-to-template-your-node-application 
を持っている場合は、私に他の最良の方法を提供してください
関連する問題