Skip to content

Commit 06a84df

Browse files
Merge pull request #230 from JocaPC/master
Updated Node.js todo sample
2 parents e1dda96 + 937951f commit 06a84df

File tree

7 files changed

+63
-134
lines changed

7 files changed

+63
-134
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ node_modules/*
22
bin/*.dll
33
obj/*
44
*.sln
5-
*.log
5+
*.log
6+
config/Development.json
7+
config/Production.json

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ This project contains an example implementation of NodeJS REST API with CRUD ope
1717

1818
- **Applies to:** SQL Server 2016 (or higher), Azure SQL Database
1919
- **Key features:** JSON Functions in SQL Server 2016/Azure SQL Database - FOR JSON and OPENJSON
20-
- **Programming Language:** JavaScript (NodeJS)
20+
- **Programming Language:** JavaScript (NodeJS), T-SQL
2121
- **Authors:** Jovan Popovic
2222

2323
<a name=before-you-begin></a>
@@ -31,7 +31,6 @@ To run this sample, you need the following prerequisites.
3131
1. SQL Server 2016 (or higher) or an Azure SQL Database
3232
2. Node.js runtime.
3333

34-
3534
**Azure prerequisites:**
3635

3736
1. Permission to create an Azure SQL Database
@@ -40,26 +39,30 @@ To run this sample, you need the following prerequisites.
4039

4140
## Run this sample
4241

43-
1. Navigate to the folder where you have downloaded sample and run **npm install** in command window.
42+
1. Navigate to the folder where you have downloaded sample and run **npm update** in the command window.
4443

4544
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.
47-
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:
49-
1. SERVERNAME - name of the database server.
50-
2. DATABASE - Name of database where Todo table is stored.
51-
3. USERNAME - SQL Server login that can access table data and execute stored procedures.
52-
4. PASSWORD - Password associated to SQL Server login.
45+
execute setup.sql script that will create and populate Todo table in the database and create necessary stored procedures.
5346

47+
3. Locate config folder in the project and setup connection info in default.json file. The content of the file should look like:
5448
```
55-
var config = {
56-
server : "SERVER.database.windows.net",
57-
userName: "USER",
58-
password: "PASSWORD",
59-
// If you're on Azure, you will need this:
60-
options: { encrypt: true, database: 'DATABASE' }
61-
};
49+
{
50+
"connection":{
51+
"server" : "<<server name or ip>>",
52+
"userName": "<<user name>>",
53+
"password": "<<password>>",
54+
"options": { "encrypt": true, "database": "<<database name>>" }
55+
}
56+
}
6257
```
58+
Content under connection key will be passed to Tedious package, which is used to
59+
interact with SQL Database. You can find more information
60+
about the properties in this object on [Tedious site](http://tediousjs.github.io/tedious/getting-started.html).
61+
62+
As an alternative, you can put connection info into Development.json or
63+
Production.json file. This sample uses [config](https://www.npmjs.com/package/config)
64+
npm module to read configurations from a file, so you can find more information about
65+
the configuration there.
6366

6467
4. Run sample app from the command line using **node app.js**
6568
1. Open http://localhost:3000/todo Url to get list of all Todo items from a table,
@@ -73,8 +76,7 @@ execute setup.sql script that will create and populate Todo table in the databas
7376
This sample application shows how to create simple REST API service that performs CRUD operations on a simple Todo table.
7477
NodeJS REST API is used to implement REST Service in the example.
7578
1. app.js file that contains startup code.
76-
2. db.js file that contains functions that wrap Tedious library
77-
3. todo.js file that contains action that will be called on GET, POST, PUT, and DELETE Http requests.
79+
3. routes/todo.js file that contains action that will be called on GET, POST, PUT, and DELETE Http requests.
7880

7981
Service uses Tedious library for data access and built-in JSON functionalities that are available in SQL Server 2016 and Azure SQL Database.
8082

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
var express = require('express');
2+
var config = require('config');
23
var bodyParser = require('body-parser');
4+
var tediousExpress = require('express4-tedious');
35

46
var app = express();
7+
app.use(function (req, res, next) {
8+
req.query = tediousExpress(req, config.get('connection'));
9+
next();
10+
});
11+
512
app.use(bodyParser.text());
613
app.use('/todo', require('./routes/todo'));
714

815
// catch 404 and forward to error handler
916
app.use(function (req, res, next) {
10-
var err = new Error('Not Found');
17+
var err = new Error('Not Found: '+ req.method + ":" + req.originalUrl);
1118
err.status = 404;
1219
next(err);
1320
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"connection":{
3+
"server" : "<<server name or ip>>",
4+
"userName": "<<user name>>",
5+
"password": "<<password>>",
6+
"options": { "encrypt": true, "database": "<<database name>>" }
7+
}
8+
}

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

Lines changed: 0 additions & 85 deletions
This file was deleted.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
},
1313
"dependencies": {
1414
"body-parser": "^1.15.2",
15+
"config": "^1.26.1",
1516
"debug": "^2.2.0",
1617
"express": "^4.14.0",
17-
"tedious": "^1.14.0"
18+
"tedious": "^1.14.0",
19+
"express4-tedious": "^0.1.0"
1820
}
1921
}
Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,49 @@
1-
var express = require('express');
2-
var router = express.Router();
3-
4-
var db = require('../db.js');
1+
var router = require('express').Router();
52
var TYPES = require('tedious').TYPES;
63

74
/* GET task listing. */
85
router.get('/', function (req, res) {
9-
db.stream("select * from todo for json path", db.createConnection(), res, '[]');
6+
7+
req.query("select * from todo for json path")
8+
.into(res, '[]');
9+
1010
});
1111

1212
/* GET single task. */
1313
router.get('/:id', function (req, res) {
1414

15-
var conn = db.createConnection();
15+
req.query("select * from todo where id = @id for json path, without_array_wrapper")
16+
.param('id', req.params.id, TYPES.Int)
17+
.into(res, '{}');
1618

17-
var request = db.createRequest("select * from todo 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, '{}');
2019
});
2120

2221
/* POST create task. */
2322
router.post('/', function (req, res) {
2423

25-
var connection = db.createConnection();
26-
var request = db.createRequest("exec createTodo @todo", connection);
27-
28-
request.addParameter('todo', TYPES.NVarChar, req.body);
29-
30-
db.executeRequest(request, connection);
24+
req.query("exec createTodo @todo")
25+
.param('todo', req.body, TYPES.NVarChar)
26+
.exec(res);
27+
3128
});
3229

3330
/* PUT update task. */
3431
router.put('/:id', function (req, res) {
3532

36-
var connection = db.createConnection();
37-
var request = db.createRequest("exec updateTodo @id, @todo", connection);
38-
39-
request.addParameter('id', TYPES.Int, req.params.id);
40-
request.addParameter('todo', TYPES.NVarChar, req.body);
41-
42-
db.executeRequest(request, connection);
33+
req.query("exec updateTodo @id, @todo")
34+
.param('id', req.params.id, TYPES.Int)
35+
.param('todo', req.body, TYPES.NVarChar)
36+
.exec(res);
37+
4338
});
4439

4540
/* DELETE single task. */
4641
router.delete('/:id', function (req, res) {
4742

48-
var connection = db.createConnection();
49-
var request = db.createRequest("delete from todo where id = @id", connection);
43+
req.query("delete from todo where id = @id")
44+
.param('id', req.params.id, TYPES.Int)
45+
.exec(res);
5046

51-
request.addParameter('id', TYPES.Int, req.params.id);
52-
53-
db.executeRequest(request, connection);
5447
});
5548

5649
module.exports = router;

0 commit comments

Comments
 (0)