Hosting Angular 4 app with node.js not working
I am trying to host Angular 4 app with Node.js
I am following this tutorial: https://javascriptrocks.wordpress.com/2016/06/04/express-with-angular-cli-in-5-minutes/
However, when I run Node.js server, it just keeps loading and doesn't show any page. Folder locations:
-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
App.js content:
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 with:
node dist/app.js
EDIT:
I'm working now. Not sure if it is correct:
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
source to share
1 answer
In the end it worked like this:
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');
});
+1
source to share