【问题标题】:Hosting Angular 4 app with node.js not working使用 node.js 托管 Angular 4 应用程序不起作用
【发布时间】:2017-05-08 12:46:22
【问题描述】:

我正在尝试使用 Node.js 托管 Angular 4 应用程序
我正在关注本指南:https://javascriptrocks.wordpress.com/2016/06/04/express-with-angular-cli-in-5-minutes/
但是,当我启动 node.js 服务器时,它只会继续加载并且不显示任何页面。 文件夹布局:
-dist
--资产
--app.js
--index.html
--inline.xxx.bundle.js
--main.xxx.bundle.js
--polyfills.xxx.bundle.js
--styles.xxx.bundle.css
--vendor.xxx.bundle.js

app.js 内容:

var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var logger = require('morgan');

var app = express();

app.use(logger('dev'));
app.use(bodyParser.json);
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(__dirname));

app.get('/*', function (req, res) {
  res.sendFile(path.join(__dirname, 'index.html'));
});

app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

var port = process.env.PORT || 8080;
app.listen(port, function () {
  console.log('Server running at port ' + port);
});

module.exports = app;

我从以下内容开始: node dist/app.js

编辑:
我现在开始工作了。不确定它是否正确:

var express = require('express');
var app = express();
var path = require('path');

app.use(express.static(path.join(__dirname, '/dist')));

app.get('/*', function(req, res) {
  res.

sendFile(path.join(__dirname + '/dist/index.html'));
});

app.listen(8080, function () {
  console.log('App started');
});

【问题讨论】:

  • 粘贴你的 index.html
  • 您找到解决此问题的方法了吗?
  • @ContinuousError 它在我的问题的底部。

标签: node.js angular express


【解决方案1】:

最终以这种方式工作:

var express = require('express');
var app = express();
var path = require('path');

app.use(express.static(path.join(__dirname, '/dist')));

app.get('/*', function(req, res) {
  res.

sendFile(path.join(__dirname + '/dist/index.html'));
});

app.listen(8080, function () {
  console.log('App started');
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    相关资源
    最近更新 更多