Nik*_*hil 8 html javascript node.js express angularjs
我有以下文件 -
1)index.html
2)app.js(角度控制器在这里)
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);
});
});
Run Code Online (Sandbox Code Playgroud)
当我通过从文件资源管理器中单击它正常打开index.html文件时,它会按预期正常打开.
但是,当我运行routes.js时,为了获取相同的index.html文件,我得到没有角度效果的原始HTML.为什么是这样?
谢谢!
编辑:
我现在可以访问我的index.css和app.js localhost:3000/public/
.我用过app.use("/public", express.static('public'));
我的routes.js
.
但现在我只能被css
包含在我的页面中,但仍然看起来像角度不包括在内.我看不到标签.请查看上面index.html
带有标签的文件截图.
我怎么能包括角度?它不包含在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>
Run Code Online (Sandbox Code Playgroud)
编辑: 我通过包括解决了它
app.use("/node_modules", express.static('node_modules'));
Run Code Online (Sandbox Code Playgroud)
在我的routes.js文件中.所以now express也使用该文件夹,因此angular
该文件夹中的文件用于提供我的index.html文件.现在,运行我localhost:3000/
给了我预期的结果.谢谢!
看起来你的静态文件(js/css)无法通过 访问expressjs
,你可以在浏览器控制台中检查是否是 404?
顺便问一下,您是否添加了app.js
以静态方式存储的目录expressjs
,或者它是否位于与 相同的目录中index.html
?
就像是:
app.use('/js',express.static(path.join(__dirname, 'public/javascripts')));
Run Code Online (Sandbox Code Playgroud)
或者,您可以将路线添加到您的app.js
:
app.get('/js/app.js', function(req, res){
res.sendFile(__dirname + '/app.js');
});
Run Code Online (Sandbox Code Playgroud)
但不建议这样做,仅用于测试。
归档时间: |
|
查看次数: |
11604 次 |
最近记录: |