1

I am trying to get a small Angular script to work within my .html page. If I load the page as a static page in my browser, the Angular script will run correctly. If I run my node.js app which loads the same page under a res.render statement, the Angular script does not run.

My theory is that my Angular code conficts with the Express.js code running. However, I'm not sure how.

Here are my scripts.

index.html

<body>
    <!--INCLUDE - JS LIBRARIES -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js" type="text/javascript"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
        crossorigin="anonymous"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js" integrity="sha256-T0Vest3yCU7pafRw9r+settMBX6JkKN06dqBnpQ8d30="
        crossorigin="anonymous"></script>

    <!--INCLUDE - ANGULAR CONTROLLER -->
    <script>
        var app = angular.module('myApp', []);
        app.controller('formCtrl', function ($scope) {
            $scope.cucmpub = "x.x.x.x";
        });
    </script>

    <!--BODY - BOOTSTRAP PAGE SETUP -->
    <div class="container-fluid" ng-app="">
        <div class="row">
            <div class="col-md-12">

                <!--BODY - BOOTSTRAP NAVBAR -->
                <nav class="navbar navbar-default" role="navigation">
                    <div class="navbar-header">
                        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                            <span class="sr-only">Toggle navigation</span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                        </button>
                        <a class="navbar-brand" href="/">CUCM
                            <sup>2</sup>
                        </a>
                    </div>
                    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                        <ul class="nav navbar-nav">
                            <li class="active">
                                <a href="/">
                                    <img src="https://png.icons8.com/marker/office/16" title="Marker" width="16" height="16"> CSS Map</a>
                            </li>
                            <li>
                                <a href="/">
                                    <img src="https://png.icons8.com/phone/ultraviolet/20" title="Phone" width="16" height="16"> SIP2SIP</a>
                            </li>
                            <li>
                                <a href="/">
                                    <img src="https://png.icons8.com/bug/color/24" title="Bug" width="16" height="16"> Debugger</a>
                            </li>
                        </ul>
                        <ul class="nav navbar-nav navbar-right">
                            <li>
                                <a ng-href="https://{{cucmpub}}/ccmadmin/" target="_blank">{{cucmpub}}</a>
                            </li>
                        </ul>
                    </div>
                </nav>

                <!--BODY - CONTENT-->
                <div class="row">
                    <div class="col-md-12" class="pull-left">
                        <form class="form-horizontal" method="post" action="/cucmmapper/submit" id="cucmmapper">
                            <!-- INPUT - TEXT-->
                            <div class="form-group">
                                <label class="col-md-4 control-label" for="cucmpub">CUCM Publisher</label>
                                <div class="col-md-4">
                                    <input id="cucmpub" name="cucmpub" type="text" placeholder="x.x.x.x" class="form-control input-md" required="" ng-model="cucmpub">
                                </div>
                            </div>
                            <!-- INPUT - SELECT-->
                            <div class="form-group">
                                <label class="col-md-4 control-label" for="cucmpub">CUCM Version</label>
                                <div class="col-md-4">
                                    <select class="form-control" id="cucmversion" name="cucmversion">
                                        <option>11.5</option>
                                        <option>11.0</option>
                                    </select>
                                </div>
                            </div>
                            <!-- INPUT - TEXT-->
                            <div class="form-group">
                                <label class="col-md-4 control-label" for="username">AXL Username</label>
                                <div class="col-md-4">
                                    <input id="username" name="username" type="text" placeholder="username" class="form-control input-md" required="">
                                </div>
                            </div>
                            <!-- INPUT - PASSWORD-->
                            <div class="form-group">
                                <label class="col-md-4 control-label" for="password">Password</label>
                                <div class="col-md-4">
                                    <input id="password" name="password" type="password" placeholder="password" class="form-control input-md" required="">
                                </div>
                            </div>
                            <!-- INPUT - BUTTON -->
                            <div class="form-group">
                                <label class="col-md-4 control-label" for="submit"></label>
                                <div class="col-md-4">
                                    <button class="btn btn-primary" type="submit">Map it!</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

</body>

app.js

// APP - INCLUDE
const express = require('express')
const path = require("path")
const bodyParser = require("body-parser")
const hbs = require('hbs')
var xml2js = require('xml2js');
var parser = new xml2js.Parser();
var opn = require('opn');

// APP - DEFINITION
const app = express()

// APP - BUILD
app.use(express.static('public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.engine('html', require('hbs').__express);
app.set('view engine', 'html');

// EXPRESS ROUTE - INDEX
app.get('/', function (req, res) {
  res.render('index.html', {
    title: 'CUCM 2.0'
  });
})

// EXPRESS ROUTING - INCLUDE - CUCM MAPPER
var routingextensions = require('./routes/cucmmapper.js')(app);

// APP - START
app.listen(3000, function () {
  console.log('Please keep this terminal open until finished as it is running the backend API communications with CUCM.')
});

opn('http://localhost:3000');

I am still a newbie at all of this and greatly appreciate any help or suggestions. Thanks y'all!

Rob
  • 14,746
  • 28
  • 47
  • 65
Andrew Petersen
  • 163
  • 1
  • 9
  • 4
    What does 'script does not run' mean? Is there an error? Newbie or not, the question needs clear problem statement in order to be on topic. – Estus Flask Oct 24 '17 at 19:55
  • 1
    I thought the question was pretty clear. It's a standard issue with static files in Node. – gyc Oct 24 '17 at 20:08
  • Did you look at the network log in browser console? All of your linked resources from the index.html are probably getting status 404 not found (or maybe 403 forbidden), correct? That would be because you are rendering one resource, namely index.html, but the rest of the site is not available through static hosting. See gycs answer, and also https://stackoverflow.com/questions/22974294/nodejs-index-html-loads-but-links-scripts-404 and https://stackoverflow.com/questions/16432847/node-rendered-html-file-not-finding-relative-path-scripts and other related Q&A about static hosting in Nodejs – ThisClark Oct 24 '17 at 20:09
  • @ThisClark thanks for the help. I did look at my network log and everything came through as status messages of 200s. – Andrew Petersen Oct 25 '17 at 14:20

1 Answers1

1

Node doesn't find the 'public' folder.

Try this line instead:

app.use(express.static(__dirname + '/public'));

https://nodejs.org/docs/latest/api/modules.html#modules_dirname

In addition, if you ever use a package manager for your libraries (instead of cdn) you will have to add this line:

app.use('/bower_components', express.static(__dirname + '/bower_components'));
gyc
  • 4,300
  • 5
  • 32
  • 54
  • thanks for the response! I appreciate it. Follow-up question though, I load all my libraries via CDN. Do you recommend relocating them locally or via bower? – Andrew Petersen Oct 25 '17 at 14:25
  • @AndrewPetersen Yes, if a cdn goes down it's better to have them locally. Response time is also faster and you can manage the versions easily. Use npm or bower as you want. I gather bower is getting outdated – gyc Oct 26 '17 at 12:07
  • Got it! Thanks for the direction. That answer worked like a charm. Thanks again! – Andrew Petersen Oct 29 '17 at 21:22