JS Framework

Express.js

13min

Hosting your Node.JS web application on Back4App servers

Introduction

This tutorial explains how you can set up a subdomain and easily host static pages. After completing this step-by-step guide, you will be able to use a Node JS App to Register and Login Users.

Prerequisites

To complete this tutorial, you will need:

  • If you want to test this App locally, you need to install Node JS in your local environment. You can follow the
  • An app created with Back4App.
    • Check out
  • Back4App Command Line Configured with the project.
    • Go through the

First off, let’s talk about our new simple application!

We’ll describe an example about the usage of Web hosting with Node JS!

Let’s imagine that we need to build a simple Log in, Register and request password from Users included in your Back4App Dashboard. We will be able to use Bootstrap, Static files (CSS and Images) and usign Express in the Project.

See the live app here: nodeapplication.back4app.io.

You can clone this complete application in the App code templates on the Back4App Platform here.

Firstly, complete the set up using the Command Line Interface (see prerequisites), to understand how it will work with the final structure from the files:

β”œβ”€β”€ NodeJSWebApp/ β”‚ β”œβ”€β”€ cloud/ β”‚ β”‚ β”œβ”€β”€ app.js β”‚ β”‚ β”œβ”€β”€ routes.js β”‚ β”‚ β”œβ”€β”€ package.json β”‚ β”‚ β”œβ”€β”€ views/ β”‚ β”‚ β”‚ β”œβ”€β”€ head.ejs β”‚ β”‚ β”‚ β”œβ”€β”€ index.ejs β”‚ β”‚ β”‚ β”œβ”€β”€ reset_password.ejs β”‚ β”œβ”€β”€ public/ β”‚ β”‚ β”œβ”€β”€ images/ β”‚ β”‚ β”œβ”€β”€ css/ β”‚ β”‚ β”‚ β”œβ”€β”€ style.css

1 - Enable your subdomain name on Back4app

Enable your Web Hosting feature, please follow the steps available in the guide below:

2 - Create and upload files

Before getting started with this step, we recommend you using Command Line Tool (see prereqs) to upload your files easily!

First off, create the files called as app.js and package.json inside the cloud directory!

3 - Create the views to your App

We’ll provide template EJS files to make the template App, you can change it anytime at your end. :)

Returning to the terminal

Inside the Cloud directory, it is necessary to create the views folder and the following EJS files:

  • ο»Ώhead.ejs- We will use it to add content to head of HTML structure.
  • ο»Ώindex.ejs- We will use it to Register and Log in users.
  • ο»Ώreset_password.ejs- We will use it for the user request the Password Reset.

Hint: We will construct the views using Bootstrap, click here to read more about it.

You can add content to your respective views. You can use the below templates without any hassle:

head.ejs
index.ejs
reset_password.ejs
ο»Ώ

4 - Create the routes to render the views

Now, we need to configure the Routes to render the views that were created previously. The routes will be built using Express.

Node.js
ο»Ώ

Hint: As you can see, we are configuring variable as parameters, which will be used to display alerts on the page.

5 - Create the routes to save the informations to your database

We will use the Parse Server Javascript Guide as a reference for developing our functions for Register, Login and Request the Password.

JS
ο»Ώ

6 - Almost there! Static files in public folder

It’s not over yet! In the public folder, you can insert static files such as CSS and images to require this on the views :)

When you add files to CSS and images, you are able to provide different stylesheets to your Website created!

7 - It’s done!

Up to this point, you have splendidly learned how to create a Node JS Application using Cloud code.

Click on this link to access the complete Project in Back4App at anytime.

With the steps described above, you’ll be able to work with the Web Hosting when using a Node JS application!