Samy Sammour
Samy Sammour

Reputation: 2465

Starting Angular application as a default of a NodeJS project

I have a NodeJS application and an Angular 6 as a frontend.

The project looks like:

-> Node Project
   ---> src 
       ---> Client_App (Anuglar)

To run the application, I need to follow those commands and start the server and angular separately, like:

-> node start
-> cd src/Client_App
-> ng serve

I need to start the two application with one single command or to add my dist file of Angular to be run at the start of my NodeJS, which is using Jade right now.

I am still new to NodeJS and still don't know how to configure it.

Anybody can help? Thanks

Edited:

I have tried now to add the dist folder to my views folder and run it within the app.js

app.get('/', function (req, res, next) {
  res.sendFile(path.join(__dirname + '/app_server/views/ngapp/index.html'));
});

But I am receiving the error, that my .js and .css folders are not found: enter image description here

Upvotes: 0

Views: 834

Answers (2)

senthil balaji
senthil balaji

Reputation: 628

You can create a new route and pass in app.route as express.static as below,

var express = require('express');

var path = require('path');
var bodyParser = require('body-parser');
var app = express();
app.use(express.static(path.join(__dirname, 'dist')));

make sure, u have build version of angular application by running this command,

ng build --prod --build-optimizer

You would need express to install in this case. express has amazing ways to handle all this

Upvotes: 0

user4676340
user4676340

Reputation:

When you build your application with the CLI ng build --prod, you get a dist folder : this folder contains all of your application, bundled into different files (feel free to look at them).

To be able to create a .ZIP file with that, you will need two things :

  • this dist folder
  • an http server

You have the first one, but not the second one.

All you need is a very simple server. For instance, http-server on NPM can do that. By installing it as a dev dependency, you could create a command in your package.json file

"deploy-locally": "http-server ./dist"

And now run it with

npm run deploy-locally

Or even better,

"start": "http-server ./dist"

And run with

npm start

If you don't want to use a NPM package (or forced to use NodeJS), simply create a basic http server in a JS file and run it with your command line (sorry, can't help on that, not into nodeJS right now).

Upvotes: 2

Related Questions