Skip to content

Commit 555b9ed

Browse files
committed
Refactored node todo sample
1 parent a5139e0 commit 555b9ed

File tree

6 files changed

+66
-55
lines changed

6 files changed

+66
-55
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
// Use IntelliSense to learn about possible Node.js debug attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"program": "${workspaceRoot}\\bin\\www",
12+
"cwd": "${workspaceRoot}"
13+
},
14+
{
15+
"type": "node",
16+
"request": "attach",
17+
"name": "Attach to Process",
18+
"port": 5858
19+
}
20+
]
21+
}

samples/features/json/todo-app/nodejs-express4-rest-api/README.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ To run this sample, you need the following prerequisites.
2929
**Software prerequisites:**
3030

3131
1. SQL Server 2016 (or higher) or an Azure SQL Database
32-
2. Visual Studio 2015 (or higher) with the NodeJS
32+
2. Node.js runtime.
33+
3334

3435
**Azure prerequisites:**
3536

@@ -39,13 +40,12 @@ To run this sample, you need the following prerequisites.
3940

4041
## Run this sample
4142

42-
1. Navigate to the folder where you have downloaded sample and run **npm install** in command window, or run setup.bat if you are on Windows operating system. This command will install necessary npm packages defined in project.json.
43-
44-
2. From SQL Server Management Studio or SQL Server Data Tools connect to your SQL Server 2016 or Azure SQL database and execute setup.sql script that will create and populate Todo table in the database.
43+
1. Navigate to the folder where you have downloaded sample and run **npm install** in command window.
4544

46-
3. From Visual Studio, open the **TodoApp.xproj** file from the root directory,
45+
2. From SQL Server Management Studio or SQL Server Data Tools connect to your SQL Server 2016 or Azure SQL database and
46+
execute setup.sql script that will create and populate Todo table in the database.
4747

48-
4. Locate db.js file in the project, change database connection info in createConnection() method to reference your database. the following tokens should be replaced:
48+
3. Locate db.js file in the project, change database connection info in createConnection() method to reference your database. the following tokens should be replaced:
4949
1. SERVERNAME - name of the database server.
5050
2. DATABASE - Name of database where Todo table is stored.
5151
3. USERNAME - SQL Server login that can access table data and execute stored procedures.
@@ -61,10 +61,9 @@ To run this sample, you need the following prerequisites.
6161
};
6262
```
6363

64-
5. Build project using Ctrl+Shift+B, right-click on project + Build, or Build/Build Solution from menu.
65-
66-
6. Run sample app using F5 or Ctrl+F5. /todo Url will be opened with a list of all Todo items as a JSON array,
67-
1. Open /api/Todo/1 Url to get details about a single Todo item with id 1,
64+
4. Run sample app from the command line using **node app.js**
65+
1. Open http://localhost:3000/todo Url to get list of all Todo items from a table,
66+
2. Open http://localhost:3000/todo/1 Url to get details about a single Todo item with id 1,
6867
2. Send POST, PUT, or DELETE Http requests to update content of Todo table.
6968

7069
<a name=sample-details></a>

samples/features/json/todo-app/nodejs-express4-rest-api/app.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,10 @@ app.use(function (req, res, next) {
1111
err.status = 404;
1212
next(err);
1313
});
14+
app.set('port', process.env.PORT || 3000);
15+
16+
var server = app.listen(app.get('port'), function() {
17+
console.log('Express server listening on port ' + server.address().port);
18+
});
1419

1520
module.exports = app;
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
#!/usr/bin/env node
22
var debug = require('debug')('nodejs_express4_rest_api');
33
var app = require('../app');
4-
5-
app.set('port', process.env.PORT || 3000);
6-
7-
var server = app.listen(app.get('port'), function() {
8-
debug('Express server listening on port ' + server.address().port);
9-
});

samples/features/json/todo-app/nodejs-express4-rest-api/db.js

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,40 +30,47 @@ function createRequest(query, connection) {
3030

3131
function stream (query, connection, output, defaultContent) {
3232

33-
errorHandler = function (ex) { throw ex; };
3433
var request = query;
3534
if (typeof query == "string") {
3635
request = this.createRequest(query, connection);
3736
}
38-
37+
pipe(request, output, defaultContent);
38+
}
39+
40+
function pipe (request, output, defaultContent) {
3941
var empty = true;
4042
request.on('row', function (columns) {
4143
empty = false;
4244
output.write(columns[0].value);
4345
});
44-
46+
4547
request.on('done', function (rowCount, more, rows) {
46-
if (empty) {
47-
output.write(defaultContent);
48-
}
49-
output.end();
48+
_OnDone(empty, defaultContent, output);
5049
});
51-
50+
5251
request.on('doneProc', function (rowCount, more, rows) {
53-
if (empty) {
52+
_OnDone(empty, defaultContent, output);
53+
});
54+
}
55+
56+
function _OnDone(empty, defaultContent, output) {
57+
if(empty) {
5458
output.write(defaultContent);
55-
}
56-
output.end();
57-
});
58-
59-
connection.on('connect', function (err) {
60-
if (err) {
61-
throw err;
62-
}
63-
connection.execSql(request);
64-
});
59+
}
60+
try { output.end(); } catch (err) { /* Ignore stream close error. */ }
61+
}
62+
63+
function executeRequest (request, connection) {
64+
connection.on('connect', function (err) {
65+
if (err) {
66+
throw err;
67+
}
68+
connection.execSql(request);
69+
});
6570
}
6671

6772
module.exports.createConnection = createConnection;
68-
module.exports.createRequest = createRequest;
69-
module.exports.stream = stream;
73+
module.exports.createRequest = createRequest;
74+
module.exports.executeRequest = executeRequest;
75+
module.exports.stream = stream;
76+
module.exports.pipe = pipe;

samples/features/json/todo-app/nodejs-express4-rest-api/routes/todo.js

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,7 @@ router.post('/', function (req, res) {
2727

2828
request.addParameter('todo', TYPES.NVarChar, req.body);
2929

30-
connection.on('connect', function (err) {
31-
if (err) {
32-
throw err;
33-
}
34-
connection.execSql(request);
35-
});
30+
db.executeRequest(request, connection);
3631
});
3732

3833
/* PUT update task. */
@@ -44,12 +39,7 @@ router.put('/:id', function (req, res) {
4439
request.addParameter('id', TYPES.Int, req.params.id);
4540
request.addParameter('todo', TYPES.NVarChar, req.body);
4641

47-
connection.on('connect', function (err) {
48-
if (err) {
49-
throw err;
50-
}
51-
connection.execSql(request);
52-
});
42+
db.executeRequest(request, connection);
5343
});
5444

5545
/* DELETE single task. */
@@ -60,12 +50,7 @@ router.delete('/:id', function (req, res) {
6050

6151
request.addParameter('id', TYPES.Int, req.params.id);
6252

63-
connection.on('connect', function (err) {
64-
if (err) {
65-
throw err;
66-
}
67-
connection.execSql(request);
68-
});
53+
db.executeRequest(request, connection);
6954
});
7055

7156
module.exports = router;

0 commit comments

Comments
 (0)