Skip to content

Commit c930f62

Browse files
Merge pull request #211 from JocaPC/master
Added new sample - nodejs, react, and SQL Server
2 parents 0d1daa5 + 1630c3a commit c930f62

File tree

11 files changed

+442
-0
lines changed

11 files changed

+442
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules/*
2+
bin/*.dll
3+
obj/*
4+
*.sln
5+
*.log
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var express = require('express');
2+
var bodyParser = require('body-parser');
3+
4+
var app = express();
5+
app.use(express.static('wwwroot'));
6+
app.use(bodyParser.urlencoded());
7+
app.use('/api/comments', require('./routes/comments'));
8+
9+
// catch 404 and forward to error handler
10+
app.use(function (req, res, next) {
11+
var err = new Error('Not Found');
12+
err.status = 404;
13+
next(err);
14+
});
15+
app.set('port', process.env.PORT || 3000);
16+
17+
var server = app.listen(app.get('port'), function() {
18+
console.log('Express server listening on port ' + server.address().port);
19+
});
20+
21+
module.exports = app;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
var debug = require('debug')('nodejs_express4_rest_api');
3+
var app = require('../app');
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
function createConnection() {
2+
3+
var config = {
4+
server : "<SERVER>",
5+
userName: "<USERNAME>",
6+
password: "<PASSWORD>",
7+
// If you're on Azure, you will need this:
8+
options: { encrypt: true, database: 'CommentsDb' }
9+
};
10+
11+
var Connection = require('tedious').Connection;
12+
var connection = new Connection(config);
13+
14+
return connection;
15+
}
16+
17+
function createRequest(query, connection) {
18+
var Request = require('tedious').Request;
19+
var req =
20+
new Request(query,
21+
function (err, rowCount) {
22+
if (err) {
23+
console.trace(err);
24+
throw err;
25+
}
26+
connection && connection.close();
27+
console.log('Connection closed');
28+
});
29+
return req;
30+
}
31+
32+
function stream (query, connection, output, defaultContent) {
33+
34+
var request = query;
35+
if (typeof query == "string") {
36+
request = createRequest(query, connection);
37+
}
38+
39+
var empty = true;
40+
request.on('row', function (columns) {
41+
if(empty) {
42+
console.log('Response fetched from SQL Database!');
43+
empty = false;
44+
}
45+
output.write(columns[0].value);
46+
});
47+
48+
request.on('done', function (rowCount, more, rows) {
49+
_OnDone(empty, defaultContent, output);
50+
});
51+
52+
request.on('doneProc', function (rowCount, more, rows) {
53+
_OnDone(empty, defaultContent, output);
54+
});
55+
56+
executeRequest (request, connection);
57+
}
58+
59+
function _OnDone(empty, defaultContent, output) {
60+
if(empty) {
61+
output.write(defaultContent);
62+
console.log('No results from database - default content is returned.');
63+
}
64+
try {
65+
console.log('Closing Http Response output.');
66+
output.end();
67+
} catch (err) {
68+
console.error(err);
69+
}
70+
}
71+
72+
function executeRequest (request, connection) {
73+
connection.on('connect', function (err) {
74+
if (err) {
75+
console.trace(err);
76+
throw err;
77+
}
78+
connection.execSql(request);
79+
});
80+
}
81+
82+
module.exports.createConnection = createConnection;
83+
module.exports.createRequest = createRequest;
84+
module.exports.executeRequest = executeRequest;
85+
module.exports.stream = stream;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "nodejs-express4-rest-api",
3+
"version": "0.0.0",
4+
"private": true,
5+
"scripts": {
6+
"start": "node ./bin/www"
7+
},
8+
"description": "nodejs-express4-rest-api",
9+
"author": {
10+
"name": "Jovan Popovic",
11+
"email": "[email protected]"
12+
},
13+
"dependencies": {
14+
"body-parser": "^1.15.2",
15+
"debug": "^2.2.0",
16+
"express": "^4.14.0",
17+
"tedious": "^1.14.0"
18+
}
19+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var express = require('express');
2+
var router = express.Router();
3+
4+
var db = require('../db.js');
5+
var TYPES = require('tedious').TYPES;
6+
7+
/* GET comments. */
8+
router.get('/', function (req, res) {
9+
db.stream("select * from comments for json path", db.createConnection(), res, '[]');
10+
});
11+
12+
/* GET single comment. */
13+
router.get('/:id', function (req, res) {
14+
15+
var conn = db.createConnection();
16+
17+
var request = db.createRequest("select * from comments where id = @id for json path, without_array_wrapper", conn);
18+
request.addParameter('id', TYPES.Int, req.params.id);
19+
db.stream(request, conn, res, '{}');
20+
});
21+
22+
/* POST create comment. */
23+
router.post('/', function (req, res) {
24+
25+
var connection = db.createConnection();
26+
var request = db.createRequest("insert into Comments values (@author, @text)", connection);
27+
28+
request.addParameter('author', TYPES.NVarChar, req.body.author);
29+
request.addParameter('text', TYPES.NVarChar, req.body.text);
30+
31+
db.executeRequest(request, connection);
32+
});
33+
34+
module.exports = router;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--CREATE DATABASE TodoDb;
2+
--USE TodoDb;
3+
DROP TABLE IF EXISTS Todo
4+
DROP PROCEDURE IF EXISTS createTodo
5+
DROP PROCEDURE IF EXISTS updateTodo
6+
GO
7+
8+
CREATE TABLE Todo (
9+
id int IDENTITY PRIMARY KEY,
10+
title nvarchar(30) NOT NULL,
11+
description nvarchar(4000),
12+
completed bit,
13+
dueDate datetime2 default (dateadd(day, 3, getdate()))
14+
)
15+
GO
16+
17+
INSERT INTO Todo (title, description, completed, dueDate)
18+
VALUES
19+
('Install SQL Server 2016','Install RTM version of SQL Server 2016', 0, '2017-03-08'),
20+
('Get new samples','Go to github and download new samples', 0, '2016-03-09'),
21+
('Try new samples','Install new Management Studio to try samples', 0, '2016-03-12')
22+
23+
GO
24+
25+
create procedure dbo.createTodo(@todo nvarchar(max))
26+
as begin
27+
insert into Todo
28+
select *
29+
from OPENJSON(@todo)
30+
WITH ( title nvarchar(30), description nvarchar(4000),
31+
completed bit, dueDate datetime2)
32+
end
33+
GO
34+
35+
create procedure updateTodo(@id int, @todo nvarchar(max))
36+
as begin
37+
update Todo
38+
set title = json.title, description = json.description,
39+
completed = json.completed, dueDate = json.dueDate
40+
from OPENJSON( @todo )
41+
WITH( title nvarchar(30), description nvarchar(4000),
42+
completed bit, dueDate datetime2) AS json
43+
where id = @id
44+
end
45+
go
46+
47+
select * from todo for json path
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
body {
2+
background: #fff;
3+
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
4+
font-size: 15px;
5+
line-height: 1.7;
6+
margin: 0;
7+
padding: 30px;
8+
}
9+
10+
a {
11+
color: #4183c4;
12+
text-decoration: none;
13+
}
14+
15+
a:hover {
16+
text-decoration: underline;
17+
}
18+
19+
code {
20+
background-color: #f8f8f8;
21+
border: 1px solid #ddd;
22+
border-radius: 3px;
23+
font-family: "Bitstream Vera Sans Mono", Consolas, Courier, monospace;
24+
font-size: 12px;
25+
margin: 0 2px;
26+
padding: 0 5px;
27+
}
28+
29+
h1, h2, h3, h4 {
30+
font-weight: bold;
31+
margin: 0 0 15px;
32+
padding: 0;
33+
}
34+
35+
h1 {
36+
border-bottom: 1px solid #ddd;
37+
font-size: 2.5em;
38+
}
39+
40+
h2 {
41+
border-bottom: 1px solid #eee;
42+
font-size: 2em;
43+
}
44+
45+
h3 {
46+
font-size: 1.5em;
47+
}
48+
49+
h4 {
50+
font-size: 1.2em;
51+
}
52+
53+
p, ul {
54+
margin: 15px 0;
55+
}
56+
57+
ul {
58+
padding-left: 30px;
59+
}
1.12 KB
Binary file not shown.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>React Tutorial</title>
6+
<!-- Not present in the tutorial. Just for basic styling. -->
7+
<link rel="stylesheet" href="css/base.css" />
8+
<script src="https://npmcdn.com/[email protected]/dist/react.js"></script>
9+
<script src="https://npmcdn.com/[email protected]/dist/react-dom.js"></script>
10+
<script src="https://npmcdn.com/[email protected]/browser.min.js"></script>
11+
<script src="https://npmcdn.com/[email protected]/dist/jquery.min.js"></script>
12+
<script src="https://npmcdn.com/[email protected]/dist/remarkable.min.js"></script>
13+
</head>
14+
<body>
15+
<div id="content"></div>
16+
<script type="text/babel" src="scripts/example.js"></script>
17+
<script type="text/babel">
18+
// To get started with this tutorial running your own code, simply remove
19+
// the script tag loading scripts/example.js and start writing code here.
20+
</script>
21+
</body>
22+
</html>

0 commit comments

Comments
 (0)