Unresolved issue with Jade in Angular Routing

Currently, I am working on building a web server using expressjs and jade for the frontend. I have also implemented client-side routing with angularjs but unfortunately, my UI is not displaying as expected. Below you can find my code and the unexpected output that I am getting:

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

app.use(express.static(__dirname + '/public'));
app.use(morgan('dev'));
app.set('view engine', 'jade');

app.get('/', function (req, res) {
    res.render("layout.jade");
});

app.listen(3030);

layout.jade

doctype html
html(ng-app="myapporder")
head
    title my orders app
    include scripts.jade
body
    block body
        h1 orders
            div(ng-view)

angapp.js

(function(){

var app = angular.module('myapporder' , ['ngRoute']);

app.config(function($routeProvider){

    $routeProvider
        .when('/' , {
            controller : 'customerController' ,
            templateUrl : 'customer'

        })
        .otherwise({ redirectTo :'/'});

  });

}());

customer.jade

div.container
    p.
        Search <input type='text' ng-model='cusfil'></input> 
    table.table.table-hover.table-bordered
        tr
            th(ng-click="dosort('name')") name
            th(ng-click="dosort('city')") city
            th(ng-click="dosort('cost')") cost
            th(ng-click="dosort('date')") date
        tr(ng-repeat='cust in customers | filter:cusfil | orderBy:sortby:reverse')
            td {{ cust.name}}
            td {{ cust.city}}
            td {{ cust.cost | currency}}
            td {{ cust.date}}
    span total customers {{customers.length}}

customercontroller.js

(function(){
var CustControl = function ($scope){
    $scope.customers=[{name:'tina' , city:'yina' , cost :4.9987 , date:'200-12-11'} , {name:'yina' , city:'yina' , cost :3.44 , date:'200-12-11'}];
    $scope.dosort = function(idw){
        $scope.sortby = idw;
        $scope.reverse = !$scope.reverse;
    };
}

angular.module('myapporder').controller('customerController' , CustControl);

}());

The issue I am facing is that instead of displaying the expected text output, it is also showing HTML tags like div, h1, etc.

Answer №1

Jade templates cannot be rendered on the client side; they require server-side rendering.

To achieve this, you can create a route in your Express server that handles rendering the template:

app.get('/template/:name', function(req, res) {
  var name = req.params.name;
    res.render('templates/' + name);
});

Once the template is rendered on the server, you can update your AngularJS route to access this URL:

$routeProvider
        .when('/' , {
            controller : 'customerController' ,
            templateUrl : 'template/customer.jade'
        })
        .otherwise({ redirectTo :'/'});
});   

Answer №2

In order to display Jade templates, the server must render them and then transmit them to the client. Angular routing typically only supports static HTML files, unless a specific request is made to the server.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble loading the socket.io.js file in electron

I am in the process of developing a chat application using Node.js, Electron, and Socket.io. However, I am encountering an issue when trying to load /socket.io/socket.io.js. Every time I attempt to do so, I receive the following error: Failed to load res ...

What is the method to extract a specific portion of a file in Node.js?

Is there a way to read a buffer in a file while streaming, by specifying both a start and end position? ...

Express Producing Empty Axios Post Request Body

I am facing an issue with sending two text data pieces from my React frontend to an Express backend. Whenever I use the post command with Axios, the body appears as {} in the backend and becomes unusable. Below is the code that I am using. Client (App.js) ...

How can you ensure that a function is only executed in AngularJS when the element is visible?

I want to execute a function only when an element is visible. I am utilizing Bootstrap to manage the element's visibility. <td class="hidden-xs col-sm-4 col-md-4 col-lg-4" ng-if="dataObject.property==false" ng-class="{'custom_class& ...

Challenges with deploying Next.js and Express with Zeit Now

I recently developed a SSR Next.js webpage with an attached Express server for handling email functionalities related to a contact form. After deploying the project on now, I encountered a 502 error when making network requests to my Express server despit ...

Creating a test suite with Jasmine for an Angular ui-grid component compiled without using $scope

I have encountered an issue while using $compile to compile a ui-grid for Jasmine testing. Initially, everything worked smoothly when I passed $scope as a parameter to the controller. However, I am now transitioning to using vm, which has resulted in $comp ...

What are the steps to turn off response data compression in an Express server?

How can I receive the 'Content-Length' response from the server without gzip compression enabled? I am using a compression middleware for this purpose: const shouldCompress = (req, res) => { if(req.headers['x-no-comprassion']) { ...

Encountered a problem when constructing a Vue/Quasar application within a Docker

Error Message While Building Docker Image with Vue-cli-service During the process of containerizing my app for deployment on a cloud service using Kubernetes, I encountered an error specifically related to the vue-cli-plugin-quasar while running the build ...

Tell me the permissions that a user possesses in discord.js

I need help creating a command using Discord.js that can display a user's permissions. For instance, the command could be $permissions @user and it should output something like: "User permissions within this guild: " I'm unsure if ...

Making HTTP requests to the localhost server using Expo SDK

I am facing an issue while trying to make HTTP requests from my Expo (React-Native) application to a node.js server running on my PC. Despite using ngrok to expose the localhost and incorporating the ngrok server in the fetch, I keep encountering a 'N ...

Access the properties of a JSON object without specifying a key

I am dealing with a collection of JSON arrays structured like this: [ { team: 111, enemyId: 123123, enemyTeam: '', winnerId: 7969, won: 1, result: '', dat ...

Seeking the location of the `onconnect` event within the Express framework

With the use of "express," I have implemented a middleware function like so: app.use(function(request, response, next) { console.log(request.headers["user-agent"]); // etc. }); This currently displays the user-agent header in the console for ever ...

Having trouble loading HTML content from another file

Here is the code snippet in question: <script> var load = function load_home(){ document.getElementById("content").innerHTML='<object type="type/html" data="/linker/templates/button.html" ></object>'; } </script> ...

Creating a simple Node.js project using material web components - a step-by-step guide

I have recently started experimenting with Node.js. This is a basic program I created to test the integration of material-components-web. I followed the instructions provided in the readme file and here's what I have implemented so far: package.json ...

The beginning of the option value is not displayed by angular.js select

While working with angular.js and bootstrap - angular-ui-bootstrap, I encountered an issue where the select element was not displaying any options until clicked, even when there were non-empty options available. My first attempt to resolve this was by dir ...

Struggling to retrieve a particular user using ExpressJs due to an error

Looking to retrieve a specific user in ExpressJS from a JSON file. Encountering this error message: UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client This is the snippet of code being u ...

Exploring ways to bypass authentication using react development tools

Currently, I am delving into the realm of token-based authentication in SPA and a question has been nagging at me. Picture this: in my app, the authentication process works like this - when a user enters correct credentials, they receive a token and the "a ...

Saving user input in free form within an Alexa skill Fact can be achieved by implementing the appropriate

I'm working on a skill that will allow users to send a support ticket to an office via Alexa. The user needs to provide their email, name, and issue in the message. However, I'm struggling with how to capture free-form text input and save it eith ...

Azure App Service for Mobile and Web applications

Currently, I am in the initial stages of assessing and designing the potential architecture for a Mobile App (using Xamarin) and a Web App (most likely using Angular). The Azure App Service appears to be a suitable option for hosting these services. Howeve ...

What is the best way to utilize ng-if in the index.html page depending on the URL?

Is there a way to hide certain elements in the index page based on the URL of nested views? In my index.html file, I am looking to implement something like this: <top-bar ng-if="!home"></top-bar> <ui-view class="reveal-animation"> ...