Skip to content

Connect a Node.js app

Introduction

In this tutorial, you'll create a simple Node.js and Express.js application and connect it to a PlanetScale database.

Prerequisites

Copied
pscale auth login

Set up the database

First, create a new database with the following command:

Copied
pscale database create <DATABASE_NAME>

Next, let's add some data to the database. You'll create a new table called users and add one record to it.

To do this, use the PlanetScale CLI shell to open a MySQL shell where you can manipulate your database. You may need to install the MySQL command line client if you haven't already.

Copied
pscale shell <DATABASE_NAME> <BRANCH_NAME>
Note
A branch, main, was automatically created when you created your database, so you can use that for BRANCH_NAME.

Create the users table:

Copied
CREATE TABLE `users` (
`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
`email` varchar(255) NOT NULL,
`first_name` varchar(255),
`last_name` varchar(255)
);

Then, add a record to it with:

Copied
INSERT INTO `users` (id, email, first_name, last_name)
VALUES (1, 'hp@example.com', 'Harry', 'Potter');

You can verify it was added with:

Copied
select * from users;
Copied
+----+----------------+------------+-----------+
| id | email | first_name | last_name |
+----+----------------+------------+-----------+
| 1 | hp@example.com | Harry | Potter |
+----+----------------+------------+-----------+

Next, you'll set up the Express starter application.

Set up the starter Node.js app

Clone the starter repository:

Copied
git clone https://github.com/planetscale/express-example.git

Enter into the folder and install the dependencies with:

Copied
cd express-example
npm install

Now that your application is set up and the database is ready to be used, let's connect them.

Connect to PlanetScale with Express.js

There are two ways to connect your Express application to your PlanetScale database: using the PlanetScale proxy or using a connection string. Both options are covered below.

Using the PlanetScale proxy with the CLI

Use the following command to create a connection to your database and start the application:

Copied
pscale connect <DATABASE_NAME> <BRANCH_NAME> --execute 'node app.js'
Note
Running pscale connect with the execute flag will pass a DATABASE_URL to the Node application, enabling it to connect to PlanetScale. Don't forget to look in app.js to see how the DATABASE_URL is used.

Navigate to http://localhost:3000 and you'll see the data from your users table!

Using a connection string

This section will show you how to create a username and password through the CLI to connect to your application. If you'd prefer to use the dashboard for this step, you can find those instructions in the Connection Strings documentation and then come back here to finish setup.

First, create your .env file by renaming the .env.example file to .env:

Copied
mv .env.example .env

Next, using the PlanetScale CLI, create a new username and password for the branch of your database:

Copied
pscale password create <DATABASE_NAME> <BRANCH_NAME> <PASSWORD_NAME>
Note
The PASSWORD_NAME value represents the name of the username and password being generated. You can have multiple credentials for a branch, so this gives you a way to categorize them. To manage your passwords in the dashboard, go to your database overview page, click "Settings", and then click "Passwords".

Take note of the values returned to you, as you won't be able to see this password again.

Copied
Password production-password was successfully created.
Please save the values below as they will not be shown again
NAME USERNAME ACCESS HOST URL ROLE PLAIN TEXT
--------------------- -------------- ----------------------------------- ------------------ -------------------------------------------------------
production-password xxxxxxxxxxxxx xxxxxx.us-east-2.psdb.cloud Can Read & Write pscale_pw_xxxxxxx

You'll use these properties to construct your connection string, which will be the value for DATABASE_URL in your .env file. Create your connection string in the following format:

Copied
mysql://<USERNAME>:<PLAIN_TEXT_PASSWORD>@<ACCESS_HOST_URL>/<DATABASE_NAME>?ssl={"rejectUnauthorized":true}

In the .env file, fill in the DATABASE_URL variable with the value you constructed. It should look something like this:

Copied
DATABASE_URL=mysql://xxxxxxxxxxxxx:pscale_pw_xxxxxxx@xxxxxx.us-east-2.psdb.cloud/express_database?ssl={"rejectUnauthorized":true}

Finally, run your Express application with:

Copied
node app.js

Navigate to http://localhost:3000 and you'll see the data from your users table!

What's next?

Learn more about how PlanetScale allows you to make non-blocking schema changes to your database tables without locking or causing downtime for production databases. If you're interested in learning how to secure your application when connecting to PlanetScale, please read Connecting to PlanetScale securely.

Need help?

Get help from PlanetScale's support team, or join our GitHub Discussion board to see how others are using PlanetScale.

Was this page useful?
Last updated on January 20, 2022
Help us improve this page
PrivacyTerms© 2022 PlanetScale Inc.