2016-10-22 26 views
1

我的公司使用SQL Server。我想为它开发报告仪表板,我想用Nodejs + Angularjs来开发它。如何显示在Angularjs中创建的网站中的数据,从SQL Server检索到的数据为Node.js

我有以下代码;你可以使用Angularjs

  • 使用Node.js的对服务器端的程序给我的

    1. 检索数据的一个小例子从SQL Server
    2. 显示HTML页面。

    我有一段代码。这不起作用。

    //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 
    
    相关问题