0

Fig 1

This is my app folder structure with the angular-client folder enter image description here

The below picture shows the issue where when I server the index file of angular app from express server , all the gets are relative to the express server app. enter image description here

This is index.js server code

var express = require('express');
var app = express();
var path = require('path');


app.listen(3000,function(){
console.log('Server started on port 3000');
});

app.get('/',function(req,res){
res.sendFile(path.join(__dirname,'angular-client')+'/src/index.html');
});

Could anyone find a solution ?

JimmyJ
  • 41
  • 9
  • Possible duplicate of [How to create a simple html server using express js](http://stackoverflow.com/questions/24517753/how-to-create-a-simple-html-server-using-express-js) – Tetsujin no Oni Feb 19 '17 at 07:08
  • I read the above question , its not the same . When serving the index file ,in the angular folder the js and css files refered are relative to the root express folder . But the path given are relative to the angular folder . I have checked many examples. All have the index file outside the src folder. Or they have two servers , one for angular and other folder node , and they run independantly. – JimmyJ Feb 19 '17 at 07:13
  • try with the path like "../../node_modules/*" – N.A Feb 19 '17 at 07:15

1 Answers1

0

The route in your image only handles GET / (and serves index.html as the response).

First, you need a route that catches GET /styles.css, for example. Then you need to have it serve the styles.css as the response.

express.static does both of these things for you (see the docs in the link).

Assuming you run node server.js from the TODOMEANAPP directory (as opposed to, say, node TODOMEANAPP/server.js), the following code should work:

app.use(express.static('angular-client'));

However, you'll have to change href="styles.css" to href="src/styles.css" and src="systemjs.config.js" to src="src/systemjs.config.js".

Adam Zerner
  • 17,797
  • 15
  • 90
  • 156
  • Actually , that not all all the file calls in the index.html should be src/ . But , what happens next is that the app components and modules are called relative to the server root and not angular root . – JimmyJ Feb 19 '17 at 08:29
  • zone.js:1960 GET http://localhost:3000/src/app/app.module 404 (Not Found) . This is the error that I got . – JimmyJ Feb 19 '17 at 08:30
  • From the express.static docs: "the path that you provide to the express.static function is relative to the directory from where you launch your node process". Suppose you run `node server.js` from `TODOMEANAPP`. Suppose you request `GET src/styles.css`. It'll hit `app.use`, `express.static` will append `src/styles.css` to `TODOMEANAPP/angular-client` and thus look for `TODOMEANAPP/angular-client/src/styles.css`, which is a file in your directory, so it'll serve that file as the response. – Adam Zerner Feb 19 '17 at 17:47
  • But if the files being served calls other files relative to its location , like the index.html file it creates a problem. I think bundling and extracting out the js files to a public folder and then serving this folder will resolve the issue . – JimmyJ Feb 20 '17 at 04:27