Connect to a Node.js app

Introduction

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

Prerequisites

pscale auth login

Set up the example Node.js app

  1. Create a new database with the following command:
pscale database create my-app
  1. Clone this repository:
git clone https://github.com/planetscale/express-example.git
  1. Run cd express-example.

  2. Run npm install.

Connect to PlanetScale with Express.js

  1. Use pscale to create a connection to your database and start the app with the following command:
pscale connect your-db-name main --execute 'node app.js'
Tip: 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.

Congratulations! You successfully connected your Node.js and Express.js application to a PlanetScale database.

Summary

In this tutorial, we created a simple Node.js and Express.js application and connected it to PlanetScale's database and branching environments.

What's next?

Now that you've successfully connected your Node.js app to PlanetScale, it's time to make schema changes to your tables and deploy your application! 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.

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 June 15, 2021
PrivacyTerms
© 2021 PlanetScale Inc.