0
projectName

    | - client/
        | - css
        | - js
        | - index.html
        | - bower_components/
    | - package.json
    | - server.js

I would like to start my app and serve index.html with node. The client is a single page AngularJs app so in server.js I checked this question and this one this is what they suggested:

var router = express();
var server = http.createServer(router);
function runServer() {
     server.listen(PORT,  () =>{
        console.log(`Our app is running on port ${ PORT }`);
    });

    router.use(express.static(path.join(__dirname, 'client')));
    router.use('/bower_components',  express.static( path.join(__dirname, 'client/bower_components')))

    router.get('*', function (req, res) {
        res.sendfile('client/index.html');
    });

}

At the head of index.html I have:

    <link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.css"/>
    <script src="/js/jquery.min.js"></script>
    <script src="/js/highstock.js"></script>
    <script src="/js/exporting.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script src="/bower_components/angular/angular.js"></script>
    <script src="/bower_components/angular-animate/angular-animate.js"></script>

When I start the server on my localhost, the app works properly. But I run the app on my host, I get the following error in the browser console.

> Uncaught SyntaxError: Unexpected token < angular-animate.js:1 Uncaught
> SyntaxError: Unexpected token < angular-route.js:1 Uncaught
> SyntaxError: Unexpected token < moment.js:1 Uncaught SyntaxError:
> Unexpected token < angular-sanitize.js:1 Uncaught SyntaxError:
> Unexpected token < frontend.js:6 Uncaught ReferenceError: angular is
> not defined

When I open the source file inside the bower_components folder in a new tab such as angular.js , I see that the serve is serving the index.html file instead of js files. But when I open the resources file in the folder css or js, such as highstock.js , I get the correct resource file.

How can I serve the files from bower_components?

TSR
  • 17,242
  • 27
  • 93
  • 197
  • Possible duplicate of [configure node express to serve static bower\_components?](https://stackoverflow.com/questions/21821773/configure-node-express-to-serve-static-bower-components) – luisenrike Jan 18 '18 at 17:58
  • @luisenrike It's not the same problem, I checked that thread before posting this question – TSR Jan 18 '18 at 18:03
  • I just tested your code and it works fine. https://gyazo.com/9cb5037403cd7dbb0908737b5fbe5e0c https://gyazo.com/8c6d53ade3efdb6043ae00ae131e1261 – luisenrike Jan 18 '18 at 18:22
  • If the file doesn't exist (angular.js) I do get those errors, so, make sure that you have the files where they need to be. – luisenrike Jan 18 '18 at 18:31

0 Answers0