Express-generator with Edge Template using Nodejs
Tags: beginners, express, node, server
Published: 2021-02-10 21:25:16


# Table of Contents
**1. [Introduction](#introduction)**
**2. [Requirements](#requirements)**
**3. [Definitions](#definitions)**
**4. [Installation](#installation)**


## Introduction

When it comes to setting up the server using **[Express](https://expressjs.com/)** you have to choose your own configuration and sometimes it takes a lot of time to achieve what you have to do.

Today we are going to talk about **[Express Generator](https://expressjs.com/en/starter/generator.html)** and how we can integrate it with the **[Edge](https://edge.adonisjs.com/)** Template engine who is not actually inbuilt into **express-generator** templates.

## Requirements
- **[Nodejs](https://nodejs.org/en/)**
You need to install Nodejs to follow this tutorial, you can check if you've already installed it by opening your terminal and enter the command `node --version` and it will give you the installed version.

## Definitions
### What is **Express**?
**[Express](https://expressjs.com)** according to his documentation is a Fast, unopinionated, minimalist web framework for Node.js.
### What is **Express generator**?
**[Express Generator](https://expressjs.com/en/starter/generator.html)** is a tool that provides an environment to quickly create a basic structure of express.
### What is **Edge**?
**[Edge Template](https://edge.adonisjs.com/)** is a logical templating engine for Node.js. This means you can write most of the Javascript expressions inside the .edge file.

## Installation
### Express Generator
To install **Express Generator** you need to go on your terminal and type the command 
``````npm install -g express-generator``````

### Generate a project
Because we've already installed express-generator now we can use the command
``````express --no-view name_of_the_project `````` to generate our project for the backend using express.
NB: We use the **"--no-view"** option because the **' Edge template '** doesn't exist inside and we need to integrate it.

After the project is generated, go inside the project directory and install the packages by running
``````cd name_of_the_project
npm install``````
### Add Edge Template Engine
Now we have generated some scaffold using express-generator we need to add 'Edge' as Template Engine.

To do this we need to install a package **[express-edge](https://www.npmjs.com/package/express-edge)**
`````` npm install express-edge `````` and after we need to register it inside our express application. Inside your **``app.js``** add the following lines:
`````` 
const { engine } = require('express-edge');
app.use(engine);
app.set('views', `${__dirname}/views`);
``````
Finally, your **``app.js``** file will look like this at the bottom.

``````
var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");
const { engine } = require("express-edge");
const bodyParser = require("body-parser");


var app = express();

app.use(engine);
app.set("views", `${__dirname}/views`);
app.use(bodyParser.json());
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));

module.exports = app;
``````
Congratulations.
We have successfully integrated the **edge template** inside our **express-generator** app.
![party-image](https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/120/whatsapp/238/face-with-party-horn-and-party-hat_1f973.png)