From the course: Angular: Creating and Hosting a Full-Stack Site

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Connect to a MySQL database from Node

Connect to a MySQL database from Node

From the course: Angular: Creating and Hosting a Full-Stack Site

Start my 1-month free trial

Connect to a MySQL database from Node

- [Instructor] So now we have MySQL installed and set up with a working database, which is pretty exciting but this won't actually do us any good yet until we know how to connect to it from our hapi server. And this is actually a fairly simple process, so let's take a look at how it's done. The first thing we have to do is to install the MySQL npm package into our project, which we can do by running npm install mysql, and hitting Enter. And next, we're going to create a new file in our src directory. And we're going to call it database.js. And this is going to contain the logic for setting up a database connection. So here's what that'll look like. We're going to start off by importing the MySQL driver like this. We're going to say import mysql from mysql. And then we're going to create a connection that our server can use to communicate with a local database we created previously. Here's what that'll look like.…

Contents