How to configure Node Express in the Angular 6 project?

Asked

Viewed 599 times

-1

I am to send a simple project that is working perfectly on my local computer, and I need to send it to the Heroku server, for this it is necessary to configure a file that will simulate Node Express in Angular, and necessary also the building of the project that I realized with the command ng build --prod.

When executing the command you created a folder called dist at the root of the project, and then set up 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);

After configuring it was necessary to execute the command node server.js to upload the application using the Node server.

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

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

This message indicates that the Node server is not finding the main project file which is the index.html, but I don’t understand why this is happening and I need help.

  • 1

    To move up an angular application run the ng build -Prod command and then move all the contents of the dist folder up to your server. You don’t need the Ode.

  • @Eduardovargas all right, but I need to know why I can’t upload the application using the settings I made in the file server.js

  • @Eduardovargas if I send the Heroku project to be configured the dist, it will end up sending to the Heroku project more than 20 megas of files and will set up to send only the dist will get lighter the project because I will only send 5 to 2 megas of file.

  • 1

    Not only send the content of dist and it should work

  • I tried not to work. gave this error message ! [remote rejected] master -> master (pre-receive hook declined) is why I need to know what’s wrong with server.js file configuration

1 answer

0

I managed by making the following changes;

I installed the express lib in the Angular project and put the server.js file this way 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');
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.