3

I am trying to host an Angular 4 application with Node.js
I am following this guide: 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. Folder layout:
-dist
--assets
--app.js
--index.html
--inline.xxx.bundle.js
--main.xxx.bundle.js
--polyfills.xxx.bundle.js
--styles.xxx.bundle.css
--vendor.xxx.bundle.js

The app.js contents:

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

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');
});
3
  • Paste your index.html Commented May 8, 2017 at 13:20
  • Did you find a solution for this issue? Commented Aug 17, 2017 at 7:58
  • @ContinuousError It's on the bottom of my question. Commented Aug 17, 2017 at 9:45

1 Answer 1

3

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');
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.