ZEIT Now, now known as Vercel, is a cloud platform that allows you to deploy serverless applications with ease. It is a great option for deploying Node.js Express apps as it allows for easy scaling and management of resources. In this tutorial, we will go through the steps to deploy a serverless Node.js Express app with ZEIT Now.
Prerequisites
- Node.js installed on your machine
- A ZEIT Now account
- Basic knowledge of Node.js and Express
Step 1: Create a Node.js Express App
First, let’s create a simple Node.js Express app. Create a new directory for the project and navigate into it:
npm init -y
npm i express
1 2 3 4 5 6 7 8 9 10 11 12 |
const express = require('express') const app = express() app.get('/', (req, res) => { res.send('Hello World!') }) const port = process.env.PORT || 3000 app.listen(port, () => { console.log(`Server running on port ${port}`) }) |
This creates a basic Express app that listens on port 3000 and sends a “Hello World!” response when you navigate to the root route.
Step 2: Test the App Locally
To test the app locally, run the following command:
node index.js
Step 3: Deploy the App with ZEIT Now
Now that we have a working app, let’s deploy it with ZEIT Now.
First, install the ZEIT Now CLI:
npm i -g now
Next, log in to your ZEIT Now account using the CLI:
now login
Navigate to the root directory of your project and run the following command to deploy the app:
now
The now
command will automatically detect that this is a Node.js app and create a serverless deployment. After a few moments, you should see a URL where your app has been deployed. Navigate to this URL in your browser to confirm that the app is running.
Step 4: Add Custom Domain
By default, your app will be deployed with a URL that looks like https://random-letters-and-numbers.now.sh
. To add a custom domain, follow these steps:
- Purchase a domain from a registrar such as Namecheap or GoDaddy.
- Add a DNS record for your domain that points to
alias.zeit.co
. This will allow ZEIT Now to verify that you own the domain. - Run the following command to add the custom domain to your deployment:
now alias <deployment-url> <custom-domain>
Replace <deployment-url>
with the URL of your deployment (e.g. https://my-express-app-abcdefg.now.sh
) and <custom-domain>
with your custom domain (e.g. mydomain.com
).
After a few moments, your custom domain should be configured and you can access your app using your custom domain.