# Build a Secure Node.js App With SQL Server

**URL:** <https://devforum.okta.com/t/build-a-secure-node-js-app-with-sql-server/17004>\
**Category:** Developer Blog Comments\
**Created:** [March 11, 2019, 4:06pm UTC](https://devforum.okta.com/t/build-a-secure-node-js-app-with-sql-server/17004 "2019-03-11T16:06:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [March 11, 2019, 4:06pm UTC](https://devforum.okta.com/t/build-a-secure-node-js-app-with-sql-server/17004/1 "2019-03-11T16:06:34Z")

</div>

[Build a Secure Node.js App With SQL Server](https://developer.okta.com/blog/2019/03/11/node-sql-server)

SQL Server is an awesome relational database. Here’s how to use it from Node.js.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [April 24, 2019, 11:42pm UTC](https://devforum.okta.com/t/build-a-secure-node-js-app-with-sql-server/17004/2 "2019-04-24T23:42:25Z")

</div>

**J. Scott Cromie**

. Hey there - do you have a working repo for this? I totally understand where you’re going with it and it looks like a lot of thought was put into it, but I am having a problem trying to connect to the DB. It’s almost as if it’s not registering the getConnection() function.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [April 26, 2019, 6:19am UTC](https://devforum.okta.com/t/build-a-secure-node-js-app-with-sql-server/17004/3 "2019-04-26T06:19:04Z")

</div>

**J. Scott Cromie**

Hey - I see the repo here at the bottom of the post…so never mind! I’ll have a look at that and see where my code differs. Thanks again for the post - it’s great!

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [April 26, 2019, 7:11pm UTC](https://devforum.okta.com/t/build-a-secure-node-js-app-with-sql-server/17004/4 "2019-04-26T19:11:01Z")

</div>

**Adan**

I am having the exact same issue… getConnection() is not defined. Not sure what’s going on here…

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [May 1, 2019, 9:26pm UTC](https://devforum.okta.com/t/build-a-secure-node-js-app-with-sql-server/17004/5 "2019-05-01T21:26:39Z")

</div>

**Adan**

Solved after days of debugging… Had my input as getPool not getConnection. smh. For some reason I thought that getConnection wasn’t being defined because of a failure to connect to SQL server but was instead a simple reference error. Sigh… #learningsql

`// getConnection was getPool - ____ -const register = async ({ sql, getConnection }) => { // read in all the .sql files for this folder const sqlQueries = await utils.loadSqlQueries(“events”); const getEvents = async userId => { // get a connection to SQL Server const cnx = await getConnection(); // create a new request const request = await cnx.request(); // configure sql query parameters request.input(“userId”, sql.VarChar(50), userId); // return the executed query return request.query(sqlQueries.getEvents); }; return { getEvents };};`

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [May 15, 2019, 3:27pm UTC](https://devforum.okta.com/t/build-a-secure-node-js-app-with-sql-server/17004/6 "2019-05-15T15:27:21Z")

</div>

**David Neal**

Apologies for the frustration! This tutorial has been corrected to use `getConnection`.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [August 13, 2019, 5:04pm UTC](https://devforum.okta.com/t/build-a-secure-node-js-app-with-sql-server/17004/7 "2019-08-13T17:04:30Z")

</div>

**remaind**

This is a great walkthrough, at least as a start…The beginning of it works great, however, I’m running into an issue integrating Okta in the last part…I’m getting a CORS error after authenticating initially:  
Access to XMLHttpRequest at 'https://[myurl].okta.com/oauth2/v1/authorize?client\_[stuff] (redirected from ‘[https://localhost:8080/api/events](https://localhost:8080/api/events)’) from origin ‘[https://localhost:8080](https://localhost:8080)’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.  
In chrome, so I think it has to do with the headers being sent by the hapi server rather than Okta. (I added a TLS cert in the hopes that maybe that was the issue). I added routes:{cors:true} to the Hapi.server config object in server.js…but it doesn’t seem like it’s doing anything.

Any insight into why this might be coming up?

Thanks again for the walk through!

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [August 14, 2019, 9:04pm UTC](https://devforum.okta.com/t/build-a-secure-node-js-app-with-sql-server/17004/8 "2019-08-14T21:04:30Z")

</div>

**David Neal**

Hi @remaind ! Thanks for taking the time to walk through the tutorial! I just stepped through the tutorial from scratch to make sure I didn’t miss anything, and it’s working for me. I believe it might be a configuration issue. Check your Okta Application Settings to see if the correct URIs are listed that match your local web application. For example: [https://www.screencast.com/…](https://www.screencast.com/t/OqnX4yqbFtIW "https://www.screencast.com/t/OqnX4yqbFtIW") Also, check your .env file settings. The Okta org url should look like [https://dev-123123.oktaprev…](https://dev-123123.oktapreview.com "https://dev-123123.oktapreview.com") with “oktapreview” instead of “okta.” The client ID and secret should be alphanumeric strings without the curly braces. Hope this helps!

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [August 15, 2019, 6:13pm UTC](https://devforum.okta.com/t/build-a-secure-node-js-app-with-sql-server/17004/9 "2019-08-15T18:13:43Z")

</div>

**remaind**

Following the instructions, I don’t have the “oktapreview” on my client dashboard…going there after following all the links included in the article takes me to a page that creates the account on .okta.com. When I set it up to go to oktapreview I get client\_id not found and I can’t sign into oktapreview either. If I follow the link that gave me issues it bounces me back to localhost. I’ve triple checked my client\_ids and app ids, they’re correct (with no quotes around them). when I click on the “log in” it takes me to the log in screen, when I authenticate, I bounce to localhost without the appearance of having logged in.

This was a great starting point for hapi and mssql, but I’ll probably just remove the okta portion and make my own provider.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [August 17, 2019, 12:07am UTC](https://devforum.okta.com/t/build-a-secure-node-js-app-with-sql-server/17004/10 "2019-08-17T00:07:30Z")

</div>

**Nick LaHam**

Very nice tutorial. I’m new to js but would like to try this. Any reason this can’t work with MSSQL Server 2008 R2? I’m in the process of upgrading our db but it’s a big undertaking at my company and I would like to build an application that can query from 2008 R2 before we complete our transition to 2016.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [August 22, 2019, 12:07am UTC](https://devforum.okta.com/t/build-a-secure-node-js-app-with-sql-server/17004/11 "2019-08-22T00:07:52Z")

</div>

**Ravindar Chandrasekar**

Hi David,

Thank you for the nice tutotrial. I have getting the error in conneting to the database below is the detail ofr the error can you please help me in resolving it.

in the browser   
{“statusCode”:500,“error”:“Internal Server Error”,“message”:“An internal server error occurred”}

in the console

c:\Projects\okta\>node .  
Server running at [http://localhost:8080](http://localhost:8080)…  
TypeError: Cannot read property ‘request’ of undefined  
 at Object.getEvents (c:\Projects\okta\src\data\events\index.js:14:34)  
 at process.\_tickCallback (internal/process/next\_tick.js:68:7)  
Debug: internal, implementation, error  
 Error: handler method did not return a value, a promise, or throw an error  
 at module.exports.internals.Manager.execute (c:\Projects\okta\node\_modules\hapi\lib\toolkit.js:48:29)  
 at process.\_tickCallback (internal/process/next\_tick.js:68:7)

Thank you

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [August 22, 2019, 3:45pm UTC](https://devforum.okta.com/t/build-a-secure-node-js-app-with-sql-server/17004/12 "2019-08-22T15:45:06Z")

</div>

**hugo**

I am having the exact same issue… getConnection(), but not sure…because @adan show one solution not working about  
so: const cnx = await getConnection() → console.log ( cnx ) = undefined …next…-\> const request = await cnx.request() → console error “Cannot read property ‘request’ of undefined”

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [August 22, 2019, 4:21pm UTC](https://devforum.okta.com/t/build-a-secure-node-js-app-with-sql-server/17004/13 "2019-08-22T16:21:53Z")

</div>

**David Neal**

Hi @ravindarchandrasekar … this is probably a configuration issue with the SQL Server connection. The code has some logging, but unfortunately I did not cover setting up a logger, such as `hapi-pino`.

As a quick fix, edit the /src/data/index.js file and add `console.log(err);` inside the catch() block of the `getConnection()` function. Should be around line 45. Restart the application and try again. You should see a more detailed error message as to why the application is not able to connect to SQL Server.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [August 22, 2019, 6:27pm UTC](https://devforum.okta.com/t/build-a-secure-node-js-app-with-sql-server/17004/14 "2019-08-22T18:27:49Z")

</div>

**David Neal**

@hugo@disqus\_PGNVpaKbow:disqus See the reply above to this question. Thanks!

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [August 22, 2019, 7:07pm UTC](https://devforum.okta.com/t/build-a-secure-node-js-app-with-sql-server/17004/15 "2019-08-22T19:07:49Z")

</div>

**hugo**

Hi David,  
Thank you for the nice tutorial. I have getting the error  
…This happens at the end of the tutorial. I have the connection with [https://developer.okta.com/](https://developer.okta.com/ "https://developer.okta.com/") as you have indicated in tutorial.  
after running npm run dev, it returns the error.  
Any help is always welcome, can you please help me in resolving it.

console: --------------------------------------------------------------------

startup error: { ValidationError: child “password” fails because [“password” is required]  
 at Object.exports.process (/home/hcgalvan/geos-nuevo-sql/node\_modules/joi/lib/errors.js:203:19)  
 at internals.Object.\_validateWithOptions (/home/hcgalvan/geos-nuevo-sql/node\_modules/joi/lib/types/any/index.js:764:31)  
 at module.exports.internals.Any.root.validate (/home/hcgalvan/geos-nuevo-sql/node\_modules/joi/lib/index.js:147:23)  
 at Object.internals.implementation [as cookie] (/home/hcgalvan/geos-nuevo-sql/node\_modules/hapi-auth-cookie/lib/index.js:98:25)  
 at module.exports.internals.Auth.\_strategy (/home/hcgalvan/geos-nuevo-sql/node\_modules/hapi/lib/auth.js:47:47)  
 at Object.module.exports.register (/home/hcgalvan/geos-nuevo-sql/src/plugins/auth.js:14:16)  
 isJoi: true,  
 name: ‘ValidationError’,  
 details:  
 [ { message: ‘“password” is required’,  
 path: [Array],  
 type: ‘any.required’,  
 context: [Object] } ],  
 \_object:  
 { password: undefined,  
 redirectTo: ‘/authorization-code/callback’,  
 isSecure: false },  
 annotate: [Function] }  
[nodemon] clean exit - waiting for changes before restart

------------------  
thank you.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [August 22, 2019, 7:22pm UTC](https://devforum.okta.com/t/build-a-secure-node-js-app-with-sql-server/17004/16 "2019-08-22T19:22:03Z")

</div>

**David Neal**

Hi @disqus\_PGNVpaKbow:disqus ,

It sounds like you may not have the SQL Server connection values configured in the `.env` file. There should be the following values configured to match your SQL database.

`SQL_USER=SQL_PASSWORD=SQL_DATABASE=SQL_SERVER=`

Hope this helps!

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [August 22, 2019, 7:32pm UTC](https://devforum.okta.com/t/build-a-secure-node-js-app-with-sql-server/17004/17 "2019-08-22T19:32:02Z")

</div>

**hugo**

@dav@reverentgeek thanks for your quick response.  
These indicated fields are with values, I will try to change them and evaluate the facts.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [August 22, 2019, 8:25pm UTC](https://devforum.okta.com/t/build-a-secure-node-js-app-with-sql-server/17004/18 "2019-08-22T20:25:21Z")

</div>

**hugo**

thank your Dave

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [August 22, 2019, 8:35pm UTC](https://devforum.okta.com/t/build-a-secure-node-js-app-with-sql-server/17004/19 "2019-08-22T20:35:59Z")

</div>

**hugo**

–

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [August 22, 2019, 9:59pm UTC](https://devforum.okta.com/t/build-a-secure-node-js-app-with-sql-server/17004/20 "2019-08-22T21:59:16Z")

</div>

**hugo**

Hi @da@reverentgeek solved!!,   
The problem related to the non-database connection was because it did not initialize before connecting to the database.  
Use the npm execution tasks: initdb and voila!  
Thanks for the patience.

[Next page](https://devforum.okta.com/t/build-a-secure-node-js-app-with-sql-server/17004.md?page=2)
