You can use ApsaraDB for RDS instances to store application data for a long period of time. This topic describes how to attach a database to a Node.js Express application and enable access to the database. For more information, see Deploy Express applications in Web+.

Environment variables

Web App Service stores information about database connections in environment variables for application access. The following table lists related environment variables.

Variable name Example value Description
WP_RDS_ENGINE MySQL The database type.
WP_RDS_CONNECTION_ADDRESS rm-*** The connection string of the database.
WP_RDS_PORT 3306 The port number that is used to connect to the database.
WP_RDS_ACCOUNT_NAME webplus The username that is used to access the database.
WP_RDS_ACCOUNT_PASSWORD A custom password The password that corresponds to the username.
WP_RDS_DATABASE webplus The name of the database.

Add a database driver

Go to a project directory where an application resides. Taking the application that is deployed in the Deploy Express applications in Web+ topic as an example, you can go to the webplus-express-app directory. Then, you can use the following command to add the MySQL database driver.
npm install mysql

Enable access to the database

Open the routes/users.js file. Modify the file as follows.
var express = require('express');
var mysql = require('mysql');

var router = express.Router();

router.get('/', function(req, res, next) {
  var connection = mysql.createConnection({
    host: process.env.WP_RDS_CONNECTION_ADDRESS,
    user: process.env.WP_RDS_ACCOUNT_NAME,
    password: process.env.WP_RDS_ACCOUNT_PASSWORD,
    database: process.env.WP_RDS_DATABASE


  connection.query('SELECT "Tom" AS user_name', function(error, results) {
    if (error) {
      throw error;
    res.send('User name queried from database: ' + results[0].user_name);

module.exports = router;

More information