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.
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:
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!
Install modules in Back4App
Inside the cloud folder (in your terminal), you need to write:
$ touch package.json
Now, insert the npm module ‘body-parser’ inside the file package.json:
JSON
1{2"dependencies":{3"body-parser":"*"4}5}
Troubleshooting: It is not necessary to run npm install inside the cloud folder because the Back4App server will install it automatically
app.js
JS
1// Require the module2var bodyParser =require("body-parser");34// Set up the views directory5 app.set('views', __dirname +'/views');6 app.set('view engine','ejs');78// Set up the Body Parser to your App9 app.use(bodyParser.json());10 app.use(bodyParser.urlencoded({extended:true}));1112//Require the routes.js file1314require('./routes');
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: