简体   繁体   中英

How to link an existing Angular 2 app to Nodejs Server?

I have an Angular 2 app with this structure :
Structure of my Angular app

Then in server side : Structure of server app

I see that there is 2 module dependencies folders, but I don't know which one I sould keep.

Here is my index.js file :

  const express = require('express');
  const path = require('path');
  const http = require('http');
  const bodyParser = require('body-parser');

  // Get our API routes
  //const api = require('./server/routes/api');

  const app = express();

  // Parsers for POST data
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false }));

  // Point static path to dist
  app.use(express.static(path.join(__dirname, 'client/portfolio')));

  // Set our api routes
  //app.use('/api', api);

  // Catch all other routes and return the index file
  app.get('*', (req, res) => {
    res.sendFile(path.join(__dirname, 'client/portfolio/src/index.html'));
  });

  /**
   * Get port from environment and store in Express.
   */
  const port = process.env.PORT || '3001';
  app.set('port', port);

  /**
   * Create HTTP server.
   */
  const server = http.createServer(app);

  /**
   * Listen on provided port, on all network interfaces.
   */
  server.listen(port, () => console.log(`API running on localhost:${port}`));

When I start server I get this message :

Loading AppComponent content here ...

But it stops here and doesn't render the Angular application.

index.html file :

<!DOCTYPE html>
<html>
   <head>
      <title>Angular QuickStart</title>
      <base href="/">
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="styles.css">
      <link href="node_modules/@angular/material/prebuilt-themes/indigo-pink.css" rel="stylesheet">
      <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
      <!-- Polyfill(s) for older browsers -->
      <script src="node_modules/core-js/client/shim.min.js"></script>
      <script src="node_modules/hammerjs/hammer.js"></script>
      <script src="node_modules/zone.js/dist/zone.js"></script>
      <script src="node_modules/systemjs/dist/system.src.js"></script>
      <script src="systemjs.config.js"></script>
      <script>
         System.import('main.js').catch(function(err){ console.error(err); });
      </script>
   </head>
   <body>
      <my-app>Loading AppComponent content here ...</my-app>
   </body>
</html>

Here's a good example of integrating AngularJS with NodeJS [ open tutorial ]

The trick is in this block

/* GET home page. */
router.get('/', function(req, res, next) {
  res.sendFile(path.join(__dirname, '../', 'views', 'index.html'));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM