Home > Enterprise >  run server angular with node express
run server angular with node express

Time:01-08

I have a problem to configure express as a server in an angular application you can enter without problems at the HOME of the app but if you click on another route, the error is displayed.

Cannot GET /

this is my conf in server.js

var express = require('express');

var app = express();

app.use('/',express.static('dist/myapp'));

app.listen(80)

I am missing something to configure?

CodePudding user response:

This is the server.js file I always use to host my Angular application. I got the code from Amazon Web Services Elastic Beanstalk.

"use strict";
const express = require("express");
const compression = require('compression');

// config
const port = process.env.PORT || 3000;
const app_folder = "./";
const options = {
  dotfiles: 'ignore',
  etag: false,
  extensions: ['html', 'js', 'scss', 'css'],
  index: false,
  maxAge: '1y',
  redirect: true,
}

// create app
const app = express();
app.use(compression());
app.use(express.static(app_folder, options));

// serve angular paths
app.all('*', function (req, res) {
    res.status(200).sendFile(`/`, {root: app_folder});
});

// start listening
app.listen(port, function () {
    console.log("Node Express server for "   app.name   " listening on http://localhost:"   port);
});
  •  Tags:  
  • Related