How to configure Node Express in Angular project 6?

-1

I'm about to send a simple project that is working perfectly on my local computer, and I need to send it to the Heroku server, so it's necessary to configure a file that will simulate the Node Express in the Angular, project that I performed with the command ng build --prod .

When executing the command was creating a folder called dist in the project root, and then I configured the server.js file as you can see below;

const express = require('express');
const app = express();

app.use(express.static(__dirname + '/dist'));

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

app.listen(4200);

Once configured, you had to run the node server.js command to upload the application using the Node server.

To verify that everything was correct I entered the URL http://localhost:4200/ and returned the following error message below;

  

C: \ Blog \ API \ api \ blog> node server.js Error: ENOENT: no such file or   directory, stat 'C: \ Blog \ API \ api \ blog. \ index.html'

This message indicates that the node server is not finding the main project file that is index.html , but I do not understand why this is happening and I need help.

    
asked by anonymous 06.09.2018 / 13:42

1 answer

0

I managed to make the following changes;

I installed the express lib in the Angular project and put the server.js file like this below;

const express = require('express');
path = require('path');

const app = express();

app.use(express.static('./dist/blog'));

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

app.listen(process.env.PORT || 4200, ()=>{
  console.log('servidor executado');
});
    
06.09.2018 / 18:34