Good. I have a couple of days with an error. I'll let you know in case anyone knows why it happens.
I have a project AngularJS v1.5.11
+ Node.js v4.2.6
(with express v4.14.1
) and when I lift it with node server.js
Angular does not recognize me.
However when I run it without Node.js (dragging it to the browser) it works fine for me. That is why I have come to the conclusion that the error is in the Express.
My folder system is this::
- server.js
- node_modules/
- public/
- ---app/
- --- assets/
- --- index.html
Server.js code (updated)
var express = require('express');
var path = require('path');
var app = express();
var port = process.env.PORT||4000;
app.use(express.static(path.join(__dirname, 'assets')));
app.get('*', function (req, res) {
res.sendFile(__dirname + '/public/index.html');
});
app.listen(port, function () {
console.log('Example app listening on port 3000!');
});
index.html code
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="utf-8">
<title>Index</title>
<script src="../server.js"></script>
<script src="app/libs/angular.js"></script>
<script src="app/libs/angular-route.js"></script>
<script src="app/js/app.js"></script>
<script src="app/js/config.js"></script>
<script src="app/js/components/testText/test-text.module.js"></script>
<script src="app/js/components/testText/test-text.component.js"></script>
<script src="app/js/components/testText/test-text.template.html"></script>
</head>
<body >
<div ng-view></div>
hola
</body>
</html>
If anyone knows what I can do. Please do not hesitate to comment.
In order for express to send static files you must set the directory containing those files. This is done through a middleware as indicated below:
The line above tells express that everything inside
public/app
are static files. In your HTML, you should only ask for them using relative paths, where/
equals the static directory: