Deploy a Node.js application with MongoDB
Deploy an Express.js application backed by a MongoDB database and verify data persistence
This guide uses a Clever Cloud example application where you can create and delete values. The application reads its connection string from MONGODB_ADDON_URI, which Clever Cloud injects when a MongoDB add-on is linked.
Prerequisites
- A Clever Cloud account
- Git
- Clever Tools, installed and connected to your account
Clone the example application
Clone the repository and move into its directory:
git clone https://github.com/CleverCloud/demo-nodejs-mongodb-rest myNodeMongoApp
cd myNodeMongoAppCreate the application and database
Create a Node.js application with the myNodeApp alias:
clever create -t node -a myNodeAppClever Tools targets your personal organisation by default. To use another organisation, add --org ORGANISATION or -o ORGANISATION when you create or link the application.
You can display your application’s URL or add a custom domain. A custom domain also requires DNS configuration:
clever domain
clever domain add your.website.tldCreate a MongoDB add-on and link it to the application:
clever addon create mongodb-addon myMongoDb -p xs_sml -l myNodeAppLinking the add-on injects its environment variables, including MONGODB_ADDON_URI, into the application. If you already have a MongoDB add-on, link it instead:
clever addon link myMongoDbDeploy the application
The example repository already contains the start script and listens on the port provided by Clever Cloud. Deploy it without additional configuration:
clever deploy
clever openAdd and delete a value from the displayed page to verify the database connection. Values are stored in MongoDB and remain available across application restarts and deployments.
MongoDB compatibility
Clever Cloud provides MongoDB 4.0.3, the last release under the GNU AGPL v3 licence. The example therefore uses Mongoose 6.x, whose bundled MongoDB driver supports this server version. Keep this compatibility constraint in mind when updating the application dependencies.