gpt4 book ai didi

javascript - 从 node.js 服务器运行 Angular

转载 作者:太空狗 更新时间:2023-10-29 13:49:30 24 4
gpt4 key购买 nike

我有以下文件 -

1) index.html

2) app.js(这里是 Angular Controller )

3) routes.js(我正在尝试从这里加载 index.html - 在 localhost :3000 上监听)

4) 索引.css

5) node_modules文件夹

路由.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);
});
});

当我通过从文件资源管理器中单击它来正常打开 index.html 文件时,它可以正常打开。

enter image description here

但是,当我运行 routes.js 以获取相同的 index.html 文件时,我得到了没有 Angular 效果的原始 HTML。 这是为什么?

enter image description here

谢谢!

编辑:

我现在可以从 localhost:3000/public/ 访问我的 index.css 和 app.js。我在我的 routes.js 中使用了 app.use("/public", express.static('public'));

但现在我只能在我的页面中包含 css,但看起来仍然没有包含 angular。我看不到标签。请查看上面带有选项卡的 index.html 文件的屏幕截图。

如何包含 Angular ?它不是包含在 HTML 本身中吗?

enter image description here

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 文件中。所以现在 express 也使用该文件夹,所以该文件夹中的 angular 文件用于为我的 index.html 文件提供服务。现在,运行我的 localhost:3000/ 会得到预期的结果。谢谢!

最佳答案

看起来您的静态文件 (js/css) 无法通过 expressjs 访问,您可以检查浏览器控制台是否为 404?

顺便问一下,您是否在 expressjs 中添加了静态存储 app.js 的目录,或者它是否位于与 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');
});

但不推荐,仅供测试。

关于javascript - 从 node.js 服务器运行 Angular ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31745871/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com