S
S
Sergey2015-07-15 09:36:29
Node.js
Sergey, 2015-07-15 09:36:29

How to process index.html?

In general, I stopped at the beginning of the beginning.
I have a project structure

app
--index.html
bower_components
server.js
, i.e. server.js is at the root, the index.php file is at the root of the app folder.
I write in server.js
var express = require('express');
var app = express();                       
var morgan = require('morgan');          
var bodyParser = require('body-parser'); 
var methodOverride = require('method-override'); 

  app.set('views', 'app');
  app.engine('html', require('ejs').renderFile);
  app.set('view engine', 'html');    

app.use(bodyParser.urlencoded({'extended': 'true'}));            // parse application/x-www-form-urlencoded
app.use(bodyParser.json());                                     // parse application/json
app.use(bodyParser.json({type: 'application/vnd.api+json'})); // parse application/vnd.api+json as json
app.use(methodOverride());

// listen (start app with node server.js) ======================================
app.listen(8080);
console.log("App listening on port 8080");

app.get('/*', function (req, res) {
    res.render('index.html'); // load the single view file (angular will handle the page changes on the front-end)
});

ejs is installed. In index.html
<!DOCTYPE html>
<!--[if lt IE 7]>      <html lang="en" ng-app="app" class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html lang="en" ng-app="app" class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html lang="en" ng-app="app" class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html lang="en" ng-app="app" class="no-js"> <!--<![endif]-->

    <head>
        <title></title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <!-- bower:css -->
        <link rel="stylesheet" href="../bower_components/angular-material/angular-material.css" />
        <!-- endbower -->
        <link href="app/core/client/css/core.css" rel="stylesheet" type="text/css">
        <link href="app/ehr/client/css/ehr.css" rel="stylesheet" type="text/css">


    </head>
    <body>
        <div ng-include="'core/client/views/header.view.client.html'"></div>
        <div ui-view class="main-inner"></div>
       
        <!-- bower:js -->
        <script src="../bower_components/angular/angular.js"></script>
        <script src="../bower_components/angular-loader/angular-loader.js"></script>
      ...
      </body>

When I run localhost:8080, I get an error in the console.
Uncaught SyntaxError: Unexpected token <
At the same time, in Network Chrome, all resources connected on this page have a status of 200.
Did I somehow set ejs up wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Zuev, 2015-07-15
@TsarS

Specify the path to static resources through express.static, for example, the project root

app.set('views', 'app');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html'); 

app.use(express.static(__dirname + '/')); <-- this

We connect resources
<link href="/app/core/client/css/core.css" rel="stylesheet" type="text/css">
<link href="/app/ehr/client/css/ehr.css" rel="stylesheet" type="text/css">

<script src="/bower_components/angular/angular.js"></script>
<script src="/bower_components/angular-loader/angular-loader.js"></script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question