从node.js服务器运行angular度
我有以下文件 –
1)index.html
2)app.js(angular度控制器在这里)
3)routes.js(我试图从这里加载index.html – 在localhost:3000上监听)
4)index.css
5)node_modules文件夹
routes.js:
var express = require('express'), app = express(), server = require('http').createServer(app), io = require('socket.io').listen(server); server.listen(3000); app.use("/node_modules", express.static('node_modules')); app.use("/public", express.static('public')); app.get('/', function(req, res){ res.sendFile(__dirname + '/index.html'); }); io.sockets.on('connection', function(socket){ socket.on('send message', function(data){ io.sockets.emit('new message', data); }); });
当我通过从文件资源pipe理器中单击打开index.html文件时,它正常打开正常。
但是,当我运行routes.js,以获取相同的index.html文件,我得到原始的HTML没有angular度的影响。 为什么是这样?
谢谢!
编辑:
我现在可以从localhost:3000/public/
访问我的index.css和app.js。 我用app.use("/public", express.static('public'));
在我的routes.js
。
但是现在我只能将css
包含在我的页面中,但是仍然看起来像angular度不包括在内。 我看不到标签。 请使用选项卡查看index.html
文件的上方屏幕截图。
我怎样才能包括angular? 是不是从HTML本身包含?
index.html –
<html> ... ... <link href="node_modules/bootstrap-3.3.5-dist/css/bootstrap.min.css" rel="stylesheet"> <link href="public/index.css" rel="stylesheet"> <body> ... ... <script src="node_modules/angular/angular.js"></script> <script src="public/ui-bootstrap-tpls-0.13.1.js"></script> <script src="node_modules/socket.io/node_modules/socket.io-client/socket.io.js"></script> <script src="public/app.js"></script> </body> </html>
编辑: 我解决了包括
app.use("/node_modules", express.static('node_modules'));
在我的routes.js文件中。 所以现在快递也使用该文件夹,所以在该文件夹angular文件用于服务我的index.html文件。 现在,运行我的localhost:3000/
给我预期的结果。 谢谢!
看起来像你的静态文件(js / css)不能通过expressjs
访问,你可以检查浏览器控制台,如果它是404?
顺便说一下,你添加app.js
存储为静态expressjs
或位于index.html
相同的目录?
就像是:
app.use('/js',express.static(path.join(__dirname, 'public/javascripts')));
另外,你可以添加一个路线到你的app.js
:
app.get('/js/app.js', function(req, res){ res.sendFile(__dirname + '/app.js'); });
但不推荐,只是为了testing。
您正在运行您的节点服务器。
所以,你已经提到了像js,css这样的静态文件的path。
为你的app.js定义包含你的控制器的path。
假设你的app.js
在根目录下的公共文件夹中,
然后在你的route.js
这样定义
app.use(express.static( '公共'));