Hello World Master

Tutorials, articles and quizzes on Software Development

A2Hosting > Articles

Create an Express Application with A2 Hosting

Lets go into our Setup Node.js app on CPanel, then we want to select the Application we worked on in the previous video


This is the project we want to setup Express on

So lets go back to CPanel and go into File Manager. Next, go to the folder containing the Express project you worked on (in my case is A2nodejs)

Next you want to create a package.json with the following information

{
  "name": "app",
  "version": "1.0.0",
  "description": "My App",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "./node_modules/parcel-bundler/bin/cli.js public/index.js --out-dir public/dist"
  },
  "dependencies": {
    "express": "^4.17.1",
    "ejs": "~2.6.1",
  },
  "author": "",
  "license": "ISC"
}

Then we want back into our nodejs a2 hosting project (via the Setup Node.js panel)

Click on the orang Stop App button

Next we want click the white NPM install button at the bottom

Let’s go back into our File Manager and back into root of the project we were working on. There, we want to create a new folder views with a file named index.ejs

Inside index.ejs, we want to put in the following

<h1>Hello Victoryflame Express</h1>

Then we want to go into our app.js file in our root directory and replace everything inside it with the following

var express = require('express');
var app = express();
var path = require('path');

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');


app.use('/a2nodejs/static', express.static('public'));

app.get('/a2nodejs', (req, res) => {
  res.render('index', { title: 'Express' });
});

app.listen();

Now go back into Setup Node.js section and restart our A2 hosting application

Go to the url of our project and you should see the html from our index.ejs being served

But what if we wanted our ejs file to have data that comes from our server.

To do that, go into your index.ejs file and replace Express from the following

<h1>Hello Victoryflame <%=title%></h1>

Now go to our application url and you should see our updated page