DEV Community

Cover image for Express-generator with Edge Template using Nodejs

Posted on

Express-generator with Edge Template using Nodejs

Table of Contents

1. Introduction
2. Requirements
3. Definitions
4. Installation


When it comes to setting up the server using Express 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 and how we can integrate it with the Edge Template engine who is not actually inbuilt into express-generator templates.


  • Nodejs 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.


What is Express?

Express according to his documentation is a Fast, unopinionated, minimalist web framework for Node.js.

What is Express generator?

Express Generator is a tool that provides an environment to quickly create a basic structure of express.

What is Edge?

Edge Template is a logical templating engine for Node.js. This means you can write most of the Javascript expressions inside the .edge file.


Express Generator

To install Express Generator you need to go on your terminal and type the command

npm install -g express-generator

Enter fullscreen mode Exit fullscreen mode

Generate a project

Because we've already installed express-generator now we can use the command

express --no-view name_of_the_project

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

 npm install express-edge

Enter fullscreen mode Exit fullscreen mode

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.set('views', `${__dirname}/views`);

Enter fullscreen mode Exit fullscreen mode

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.set("views", `${__dirname}/views`);
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, "public")));

module.exports = app;

Enter fullscreen mode Exit fullscreen mode

We have successfully integrated the edge template inside our express-generator app.

Top comments (0)