Configuring Local Machine Infrastructure
To set up the infrastructure specific to this article on your local machine, take the following steps:
- Create a new part4 directory under the project directory from part1-3.
- Open a command prompt (terminal) in the new directory.
- From the command prompt run “npm install express-generator -g“
The -g flag means to do a global rather than local installation. Express generator will quickly create an application skeleton for us, saving a lot of time building files and directories from scratch.
Type “express –no-view dbjs” (those are two dashes preceding no-view) … at the command prompt. Express will create a directory called dbjs and a skeleton project within the new directory.
Stop the node.js server process that you were running for part 3. We will start a new web server below, and it will source its web content from a different directory structure.
Follow the instructions that Express provides (this is for Windows; other platforms will be slightly different):change directory:
> cd dbjsinstall dependencies:
> npm installrun the app:
> npm start
Now open a browser to http://localhost:3000 . You should see a simple “Welcome to Express” page.
Close the browser page and CTRL+C in the command line to stop the node web server you’ve just tested.
We’ll run our node server with nodemon so that we don’t have to restart it manually every time we make a change to our server side code, so in the command line, run:
npm install nodemon -g
- We’ll need it in a minute, so install the Microsoft SQL Server client for Node.js by running “npm install mssql” from the command prompt. NPM (node package manager) is the “world’s largest software library” and this command line utility will install or update whatever libraries you need to use for your project.
We will use SQL Azure as our database. If you do not already have one, follow these instructions to set up a sample database. Don’t forget to create a firewall rule to enable connectivity to the Azure SQL instance from your current network. Also, don’t forget to save that firewall rule once you have it configured. Of course you can use a local or on-premise instance of SQL Server if you would prefer.
Adding Database Query Code
Edit the index.html file adding the following appDB.js script source line to the head and adding the following new div to the body.
<!-- Add this to the bottom of the body of the index.html file--> <div> <span id="result">This text should be replaced with data.</span> </div>
So index.html will look like this:
App.js will look like this. All but two lines were automatically created by Express Generator:
Rename the credentialsExample.js file to credentials.js, and edit the file to add your credentials and Azure SQL database name.
Now start the app with “nodemon app.js“ at your command line in the directory of our new web app.
Browse to http://localhost:3000, and if all is configured correctly you should see this:
What have we done here?
The app.js file is used to configure our little web app. A route is used by Express to direct requests. App.js already had routes configured for the root (‘/’, when you browse to localhost:3000) and /users (as in localhost:3000/users) from our initial setup. We don’t have any code implemented to handle a request to /users, so nothing will happen at that url. We added a route for /getData, specifying that when it is called, the code in our db.js file is to be run.
When we browse to http://localhost:3000, our Node server responds by looking up the route for ‘/’, the root of our site, and passing that request to ./routes/index as configured in app.js. This causes the server to load and run ./routes/index.js, which renders index.html to the browser.
The browser receives the index.html document from the server and parses it. From the head section, it invokes appDB.js. The browser retrieves that file from the server and executes it. In the fetchData function of appDB.js, we see fetch(“/getData”… which tells the browser to call back (using a POST request as opposed to a GET request) to the server at the localhost:3000/getData path.
The db.js file contains our database vendor specific functionality. The code comes from a sample on the mssql npm page. This is where you would swap out the SQL Server specific code for AWS, Google, Oracle, MySQL or other database connectivity code. In this file we are using the mssql package to create a connection pool, send a query, receive the result, and close the connection.
In part 5, we will take these concepts and add them to the extension concepts from part 3 to make extension that writes back to a database.