Skip to content

Commit e5b78ee

Browse files
committed
Node.js REST APi
Draft version of NodeJS REST API that uses SQL/JSON functions
1 parent 86ce80b commit e5b78ee

File tree

9 files changed

+356
-1
lines changed

9 files changed

+356
-1
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateA
2424
*.zip
2525
samples/features/in-memory/ticket-reservations/packages/CircularGauge.1.0.0/CreatePackageFile.bat
2626
samples/features/in-memory/ticket-reservations/TicketReservations/TicketReservations.dbmdl
27-
samples/databases/wide-world-importers/workload-drivers/vehicle-location-insert/MultithreadedInMemoryTableInsert/obj/Release/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
27+
samples/databases/wide-world-importers/workload-drivers/vehicle-location-insert/MultithreadedInMemoryTableInsert/obj/Release/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
28+
*.dat
29+
*.sln

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ To work in GitHub, go to https://github.com/microsoft/sql-server-samples and for
88

99
Each sample should be in its own folder with a README.md file that follows the [template](README_samples_template.md). Generated files (e.g., .exe or .bacpac) and user configuration settings (e.g., .user) should not be committed to GitHub.
1010

11+
## Code of Conduct
12+
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
13+
1114
## License
1215
These samples and templates are all licensed under the MIT license. See the license.txt file in the root.
1316

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules/*
2+
bin/*
3+
obj/*
4+
*.sln
5+
*.log
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# NodeJS Express4 REST API that uses SQL/JSON functionalites
2+
3+
This project contains an example implementation of NodeJS REST API with CRUD operations on a simple Todo table. You can learn how to build REST API on the existing database schema using new JSON functionalities that are available in SQL Server 2016 (or higher) and Azure SQL Database.
4+
5+
### Contents
6+
7+
[About this sample](#about-this-sample)<br/>
8+
[Before you begin](#before-you-begin)<br/>
9+
[Run this sample](#run-this-sample)<br/>
10+
[Sample details](#sample-details)<br/>
11+
[Disclaimers](#disclaimers)<br/>
12+
[Related links](#related-links)<br/>
13+
14+
<a name=about-this-sample></a>
15+
16+
## About this sample
17+
18+
- **Applies to:** SQL Server 2016 (or higher), Azure SQL Database
19+
- **Key features:** JSON Functions in SQL Server 2016/Azure SQL Database - FOR JSON and OPENJSON
20+
- **Programming Language:** JavaScript (NodeJS)
21+
- **Authors:** Jovan Popovic
22+
23+
<a name=before-you-begin></a>
24+
25+
## Before you begin
26+
27+
To run this sample, you need the following prerequisites.
28+
29+
**Software prerequisites:**
30+
31+
1. SQL Server 2016 (or higher) or an Azure SQL Database
32+
2. Visual Studio 2015 (or higher) with the NodeJS
33+
34+
**Azure prerequisites:**
35+
36+
1. Permission to create an Azure SQL Database
37+
38+
<a name=run-this-sample></a>
39+
40+
## Run this sample
41+
42+
1. Navigate to the folder where you have downloaded sample and run **npm install** in command window. 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.
45+
46+
3. From Visual Studio, open the **TodoApp.xproj** file from the root directory,
47+
48+
4. Locate db.js file in the project, change database connection info in createConnection() method to reference your database, and build solution using Ctrl+Shift+B, right-click on project + Build, or Build/Build Solution from menu.
49+
50+
5. Run sample app using F5 or Ctrl+F5,
51+
4.1. Open /api/Todo Url to get all Todo items as a JSON array,
52+
4.2. Open /api/Todo/1 Url to get details about a single Todo item with id 1,
53+
4.3. Send POST, PUT, PATCH, or DELETE Http requests to update content of Todo table.
54+
55+
<a name=sample-details></a>
56+
57+
## Sample details
58+
59+
This sample application shows how to create simple REST API service that performs CRUD operations on a simple Todo table.
60+
NodeJS REST API is used to implement REST Service in the example.
61+
Service uses built-in JSON functionalities that are available in SQL Server 2016 and Azure SQL Database.
62+
63+
<a name=disclaimers></a>
64+
65+
## Disclaimers
66+
The code included in this sample is not intended demonstrate some general guidances and arhitectural patterns for web development.
67+
It contains minimal code required to create REST API.
68+
You can easily modify this code to fit the architecture of your application.
69+
70+
<a name=related-links></a>
71+
72+
## Related Links
73+
74+
For more information, see this [MSDN documentation](https://msdn.microsoft.com/en-us/library/dn921897.aspx).
75+
76+
## Code of Conduct
77+
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
78+
79+
## License
80+
These samples and templates are all licensed under the MIT license. See the license.txt file in the root.
81+
82+
## Questions
83+
Email questions to: sqlserversamples@microsoft.com.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var express = require('express');
2+
var bodyParser = require('body-parser');
3+
4+
var app = express();
5+
app.use(bodyParser.text());
6+
app.use('/todo', require('./routes/todo'));
7+
8+
// catch 404 and forward to error handler
9+
app.use(function (req, res, next) {
10+
var err = new Error('Not Found');
11+
err.status = 404;
12+
next(err);
13+
});
14+
15+
module.exports = app;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
function createConnection() {
2+
3+
var config = {
4+
server : "SERVER.database.windows.net",
5+
userName: "USER",
6+
password: "PASSWORD",
7+
// If you're on Azure, you will need this:
8+
options: { encrypt: true, database: 'DATABASE' }
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+
throw err;
24+
}
25+
connection && connection.close();
26+
});
27+
28+
return req;
29+
}
30+
31+
function stream (query, connection, output, defaultContent) {
32+
33+
errorHandler = function (ex) { throw ex; };
34+
var request = query;
35+
if (typeof query == "string") {
36+
request = this.createRequest(query, connection);
37+
}
38+
39+
var empty = true;
40+
request.on('row', function (columns) {
41+
empty = false;
42+
output.write(columns[0].value);
43+
});
44+
45+
request.on('done', function (rowCount, more, rows) {
46+
if (empty) {
47+
output.write(defaultContent);
48+
}
49+
output.end();
50+
});
51+
52+
request.on('doneProc', function (rowCount, more, rows) {
53+
if (empty) {
54+
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+
});
65+
}
66+
67+
module.exports.createConnection = createConnection;
68+
module.exports.createRequest = createRequest;
69+
module.exports.stream = stream;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">11.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
<Name>nodejs-express4-rest-api</Name>
7+
<RootNamespace>nodejs-express4-rest-api</RootNamespace>
8+
<LaunchUrl>todo</LaunchUrl>
9+
</PropertyGroup>
10+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
11+
<PropertyGroup>
12+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
13+
<SchemaVersion>2.0</SchemaVersion>
14+
<ProjectGuid>da18f211-dee4-4b4a-8954-4c7505dc769b</ProjectGuid>
15+
<ProjectHome>.</ProjectHome>
16+
<StartupFile>bin\www</StartupFile>
17+
<SearchPath>
18+
</SearchPath>
19+
<WorkingDirectory>.</WorkingDirectory>
20+
<OutputPath>.</OutputPath>
21+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
22+
<ProjectTypeGuids>{3AF33F2E-1136-4D97-BBB7-1795711AC8B8};{349c5851-65df-11da-9384-00065b846f21};{9092AA53-FB77-4645-B42D-1CCCA6BD08BD}</ProjectTypeGuids>
23+
<ProjectView>ShowAllFiles</ProjectView>
24+
<NodejsPort>1337</NodejsPort>
25+
<StartWebBrowser>True</StartWebBrowser>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
28+
<DebugSymbols>true</DebugSymbols>
29+
</PropertyGroup>
30+
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
31+
<DebugSymbols>true</DebugSymbols>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Compile Include="app.js" />
35+
<Compile Include="db.js" />
36+
<Compile Include="routes\todo.js" />
37+
<Compile Include="bin\www" />
38+
<Content Include="package.json" />
39+
<Content Include="README.md" />
40+
</ItemGroup>
41+
<ItemGroup>
42+
<Folder Include="bin\" />
43+
<Folder Include="util\" />
44+
<Folder Include="public\" />
45+
<Folder Include="routes\" />
46+
</ItemGroup>
47+
<!-- Do not delete the following Import Project. While this appears to do nothing it is a marker for setting TypeScript properties before our import that depends on them. -->
48+
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets" Condition="False" />
49+
<Import Project="$(VSToolsPath)\Node.js Tools\Microsoft.NodejsTools.targets" />
50+
<ProjectExtensions>
51+
<VisualStudio>
52+
<FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
53+
<WebProjectProperties>
54+
<UseIIS>False</UseIIS>
55+
<AutoAssignPort>True</AutoAssignPort>
56+
<DevelopmentServerPort>0</DevelopmentServerPort>
57+
<DevelopmentServerVPath>/</DevelopmentServerVPath>
58+
<IISUrl>http://localhost:48022/</IISUrl>
59+
<NTLMAuthentication>False</NTLMAuthentication>
60+
<UseCustomServer>True</UseCustomServer>
61+
<CustomServerUrl>http://localhost:1337</CustomServerUrl>
62+
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
63+
</WebProjectProperties>
64+
</FlavorProperties>
65+
<FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}" User="">
66+
<WebProjectProperties>
67+
<StartPageUrl>
68+
</StartPageUrl>
69+
<StartAction>CurrentPage</StartAction>
70+
<AspNetDebugging>True</AspNetDebugging>
71+
<SilverlightDebugging>False</SilverlightDebugging>
72+
<NativeDebugging>False</NativeDebugging>
73+
<SQLDebugging>False</SQLDebugging>
74+
<ExternalProgram>
75+
</ExternalProgram>
76+
<StartExternalURL>
77+
</StartExternalURL>
78+
<StartCmdLineArguments>
79+
</StartCmdLineArguments>
80+
<StartWorkingDirectory>
81+
</StartWorkingDirectory>
82+
<EnableENC>False</EnableENC>
83+
<AlwaysStartWebServerOnDebug>False</AlwaysStartWebServerOnDebug>
84+
</WebProjectProperties>
85+
</FlavorProperties>
86+
</VisualStudio>
87+
</ProjectExtensions>
88+
</Project>
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": "jovanpop@microsoft.com"
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 task listing. */
8+
router.get('/', function (req, res) {
9+
db.stream("select * from todo for json path", db.createConnection(), res, '[]');
10+
});
11+
12+
/* GET single task. */
13+
router.get('/:id', function (req, res) {
14+
15+
var conn = db.createConnection();
16+
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, '{}');
20+
});
21+
22+
/* POST create task. */
23+
router.post('/', function (req, res) {
24+
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+
connection.on('connect', function (err) {
31+
if (err) {
32+
throw err;
33+
}
34+
connection.execSql(request);
35+
});
36+
});
37+
38+
/* PUT update task. */
39+
router.put('/:id', function (req, res) {
40+
41+
var connection = db.createConnection();
42+
var request = db.createRequest("exec updateTodo @id, @todo", connection);
43+
44+
request.addParameter('id', TYPES.Int, req.params.id);
45+
request.addParameter('todo', TYPES.NVarChar, req.body);
46+
47+
connection.on('connect', function (err) {
48+
if (err) {
49+
throw err;
50+
}
51+
connection.execSql(request);
52+
});
53+
});
54+
55+
/* DELETE single task. */
56+
router.delete('/:id', function (req, res) {
57+
58+
var connection = db.createConnection();
59+
var request = db.createRequest("delete from todo where id = @id", connection);
60+
61+
request.addParameter('id', TYPES.Int, req.params.id);
62+
63+
connection.on('connect', function (err) {
64+
if (err) {
65+
throw err;
66+
}
67+
connection.execSql(request);
68+
});
69+
});
70+
71+
module.exports = router;

0 commit comments

Comments
 (0)