简体   繁体   English

托管Angular 4应用程序,node.js无效

[英]Hosting Angular 4 app with node.js not working

I am trying to host an Angular 4 application with Node.js 我正在尝试使用Node.js托管Angular 4应用程序
I am following this guide: https://javascriptrocks.wordpress.com/2016/06/04/express-with-angular-cli-in-5-minutes/ 我正在关注本指南: https//javascriptrocks.wordpress.com/2016/06/04/express-with-angular-cli-in-5-minutes/
However when I start the node.js server it just keeps on loading and doesnt show any page. 但是,当我启动node.js服务器时,它只是继续加载并且不显示任何页面。 Folder layout: 文件夹布局:
-dist -dist
--assets - 资产
--app.js --app.js
--index.html --index.html
--inline.xxx.bundle.js --inline.xxx.bundle.js
--main.xxx.bundle.js --main.xxx.bundle.js
--polyfills.xxx.bundle.js --polyfills.xxx.bundle.js
--styles.xxx.bundle.css --styles.xxx.bundle.css
--vendor.xxx.bundle.js --vendor.xxx.bundle.js

The app.js contents: 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;

I start it with: node dist/app.js 我从以下开始: node dist/app.js

EDIT: 编辑:
I got it working now. 我现在就开始工作了。 Not sure if its the right way: 不确定它是否正确:

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');
});

Eventually got it working this way: 最终以这种方式工作:

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');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM