How to Deploy an Express.js REST API on Render: An Alternative to … – MUO – MakeUseOf

At the end of 2022, Heroku discontinued its free tier option resulting in the removal of the previously available free subscription plan for web hosting and other cloud services in its ecosystem.

Free tiers are particularly handy and cost-effective if you intend to deploy and host applications for a short period. Luckily, there is an alternative cloud platform that, like Heroku, offers a free tier for its cloud services among other perks.

In this article, we'll explore how to deploy an Express.js REST API on Render, a cloud-based hosting platform.

Render is a cloud hosting platform that provides a seamless and hassle-free way to deploy and manage static websites, fully-fledged web applications, backend APIs, or databases on the cloud. The features include:

Like the other cloud platforms, Render has its perks and drawbacks. But how does it compare to popular cloud solutions like Heroku?

To get started, header over to Render's website and sign up and log into your account.

Render makes it easy to deploy and manage backend APIs by offering built-in support for popular programming languages and web services that streamline the deployment process.

This guide will make use of its web services feature to deploy an Express.js REST API. To follow along, you will need to first set up a PostgreSQL database instance on Render.

On the overview page, click on the New PostgreSQL button to set up a new instance.

Next, fill in the name of your database, and click on Create database. Finally, copy the Internal Database URL provided. You will use it to configure the connection between your Express REST API and the PostgreSQL database.

Essentially, the Internal Database URL is used to establish a connection between applications running on Render's servers such as a deployed API or a fully-fledged web application.

Nonetheless, if you only want to utilize the PostgreSQL database from an application deployed on another platform, you can use the External Database URL to configure the database connection.

Go ahead and create an Express.js web server. Next, install the following packages:

To set up the connection between the Express.js API and Render's PostgreSQL instance, in the root directory of your project folder, create a db.js file and add the code below.

module.exports = db;

Next, open the index.js file, and add the code below that implements a simple REST API with four routes.

app.use(express.json())app.use(express.urlencoded({ extended: true }))

app.get('/', (req, res) => res.send('Hello World!' ))

app.get('/users', async (req, res) => {try {const users = await db.select().from('users')res.json(users)} catch (error) {console.error(error)res.status(500).json({ message: 'Error retrieving users' })}})app.post('/users', async (req, res) => {try {const user = await db('users').insert({ name: req.body.name }).returning('*')res.json(user)} catch (error) {console.error(error)res.status(500).json({ message: 'Error creating user' })}})

app.delete('/users/:id', async (req, res) => {try {const { id } = req.paramsconst user = await db('users').where({ id }).delete().returning('*')res.json(user)} catch (error) {console.error(error)res.status(500).json({ message: 'Error deleting user' })}})

app.listen(PORT, () => console.log(`Server up at PORT:${PORT}`))

Create a new folder, scripts, in the root directory of your project, add a new file, migrate.js, and finally, add the code below:

This code will create a new users table in the database with two columns: an auto-incrementing primary key field and a name field.

Finally, add these commands to your package.json file.

Lastly, in order to create the user's table on the database, you need to run the migrate.js file as a script on your terminal using the command below.

However, before running the command, make sure to retrieve the External Database URL from Render's PostgreSQL instance settings information, and paste it into the db.js file as the connection string.

This will establish a connection with the database instance from your local machine, allowing you to create the table before deploying the API. Once the table is created, you can then head over to your Render's PostgreSQL instance, retrieve the Internal Database URL, and update the db.js file accordingly.

First, create a new repository on GitHub and push the project code. Next, log into your Render account, click on the New+ button, and select the Web Service option from the drop-down menu.

Finally, access your GitHub account, select your project's repository, and connect to it on Render.

On the web service settings page, provide a name for the new service, specify the root directory of the project, the build and start command, and finally, click on Create Web Service. Once the deployment process is complete, copy the provided URL to test the endpoints on Postman.

Postman is a popular tool for developing and testing APIs. To get familiar with Postman, learn how to use it to test an API.

To test the deployed API, make a POST request to the /users endpoint to store data in the PostgreSQL database.

Finally, make a GET request to retrieve the stored data.

Render provides a straightforward setup process, and seamless integration with popular version control systems making it a good alternative cloud hosting platform.

Moreover, its competitive pricing model and built-in support for popular development tools make it a reliable and user-friendly option for both side projects and large commercial applications.

See the original post here:
How to Deploy an Express.js REST API on Render: An Alternative to ... - MUO - MakeUseOf

Related Posts

Comments are closed.