Tutorial
How to Deploy a Node.js Express & MongoDB App
You’ve built your Express server and mongoose models on your local machine. Now it's time to put it on the web so a frontend SPA or mobile client can communicate with it.
Prepare Your MongoDB Connection String
Ensure that in your `server.js` or `app.js`, you are reading the database URI from the environment variables rather than hardcoding it:
mongoose.connect(process.env.MONGO_URI)
.then(() => console.log('Database connected'))
.catch(err => console.error(err));
Use the Dynamic PORT
Your cloud environment will assign a port dynamically. Allow your app to listen on the cloud's port, rather than hardcoding `3000`.
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});
Cloud Deployment
- Create an account in MongoDB Atlas (it’s free), set up your database, and get your connection string. Be sure to whitelist IP address `0.0.0.0/0` so your cloud server can connect.
- In your Remoud dashboard, create a new app from your GitHub repository.
- In your App's Settings panel, add an Environment Variable with the key `MONGO_URI` and paste the connection string you got from Atlas.
- Click Save. Your environment is secure.
- Wait for the container to build via `npm install` and start your app via `npm start`.
Start Processing API Requests
Deploy your Express APIs instantly without dealing with nginx reverse proxies or PM2 process managers.
Start deploying for free →