1

I tried to connect angular front end with nodejs earlier I ran ng build on public folder of app and then ran localhost:3000. No error was given on compilation but app is broken now. Now do I achieve it? My folder structure is like this

  • client
  • public
  • app.js

client contains all angular code, public contains compiled code of angular till I have also changed output path to ../public in angular.json. And here is my app.js:

                 var express = require('express');
                 var path= require('path');
                   var cors = require('cors');
                    var mongoose = require('mongoose');
                     var bodyParser = require('body-parser');
                    var passport = require('passport');
                     var app= express();
                   var port = 3000;
                     app.set('view engine','ejs');
                //app.use(express.static(path.join(__dirname, 'public')));
                 app.set('views', path.join(__dirname, '/views'));
                 app.use(express.static(path.join(__dirname, 'public')));
                 app.use('/api', routesApi);
                   app.get('/', (req,res) => {
                   res.send("Invalid page");
                             });
                   app.listen(port, () => {
              console.log(`Starting the server at port ${port}`);
                            });
5
  • Both of your server code(node.js) and frontend code(angular) are running without any errors? Commented Jun 5, 2018 at 5:28
  • yes before running ng build there was no error in front end or backend app was working fine Commented Jun 5, 2018 at 6:11
  • What happend after running ng build? Did you get any errors? Commented Jun 5, 2018 at 6:12
  • ng build didnt give any errors but when i run app it was broken when i copy the same code and again run it without runnnig ng build on public folder to combine them whole app is working fine i dont know what is wrong with ng build Commented Jun 5, 2018 at 6:15
  • Please read Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. Commented Jun 5, 2018 at 8:56

1 Answer 1

2

I think up to now you have a build of a running angular application and perfectly running node.js server.

What you have to do is host your client application. Follow the following steps to get this working.

  1. Install http-server globally on your machine using npm install http-server -g command
  2. Then run http-server /path/to/your/dist/folder. (You should get a dist folder after running ng build --prod)
  3. Then run your node.js server.
  4. And open your browser and go to http://localhost:8080
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.