How to Deploy Express.js to AWS: Step-by-Step Guide
To deploy an
Express.js app to AWS, use AWS Elastic Beanstalk which automates server setup. Package your app with a package.json, create an Elastic Beanstalk environment, and upload your code via the AWS CLI or console to run your Express server in the cloud.Syntax
Deploying Express to AWS Elastic Beanstalk involves these main steps:
- Prepare your app: Ensure
package.jsonandapp.js(orserver.js) are ready. - Initialize Elastic Beanstalk: Use
eb initto set up your AWS project. - Create environment: Use
eb createto launch an environment for your app. - Deploy app: Use
eb deployto upload and start your Express app on AWS.
bash
npm init -y npm install express // app.js const express = require('express'); const app = express(); const port = process.env.PORT || 3000; app.get('/', (req, res) => { res.send('Hello from Express on AWS!'); }); app.listen(port, () => { console.log(`Server running on port ${port}`); });
Example
This example shows a simple Express app deployed to AWS Elastic Beanstalk using the AWS CLI.
It demonstrates how to initialize, create an environment, and deploy your app.
bash
mkdir my-express-app cd my-express-app npm init -y npm install express // Create app.js with this content: const express = require('express'); const app = express(); const port = process.env.PORT || 3000; app.get('/', (req, res) => { res.send('Hello from Express on AWS!'); }); app.listen(port, () => { console.log(`Server running on port ${port}`); }); // Initialize Elastic Beanstalk eb init -p node.js my-express-app --region us-east-1 // Create environment eb create my-express-env // Deploy app eb deploy // Open app in browser eb open
Output
Server running on port 3000
// When visiting the URL opened by 'eb open', you see:
Hello from Express on AWS!
Common Pitfalls
Common mistakes when deploying Express to AWS Elastic Beanstalk include:
- Not setting the
PORTenvironment variable, causing the app to listen on the wrong port. - Missing
package.jsonornode_modulesnot installed before deployment. - Not initializing Elastic Beanstalk with the correct platform (
node.js). - Forgetting to commit all files or deploying from the wrong directory.
Always use process.env.PORT in your app to let AWS assign the port.
javascript
/* Wrong: Hardcoded port */ const port = 3000; /* Right: Use environment port */ const port = process.env.PORT || 3000;
Quick Reference
| Command | Purpose |
|---|---|
| npm init -y | Create package.json for your app |
| npm install express | Install Express framework |
| eb init -p node.js | Initialize Elastic Beanstalk project |
| eb create | Create Elastic Beanstalk environment |
| eb deploy | Deploy your app to AWS |
| eb open | Open deployed app in browser |
Key Takeaways
Use AWS Elastic Beanstalk to easily deploy and manage your Express app in the cloud.
Always listen on the port provided by process.env.PORT for AWS compatibility.
Initialize and create your Elastic Beanstalk environment before deploying.
Deploy your app using the AWS EB CLI commands for smooth updates.
Check your app by opening the URL Elastic Beanstalk provides after deployment.