我的公司使用SQL Server。我想为它开发报告仪表板,我想使用Nodejs + Angularjs开发它。
我有以下代码;你能给我一个小例子
吗?我有一段代码。这不起作用。
//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 :(得分: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