【问题标题】:Running Angular2 app with Node Js server使用 Node Js 服务器运行 Angular2 应用程序
【发布时间】:2016-10-12 22:28:13
【问题描述】:

我正在使用 Angular2(客户端)和 Node JS(服务器端)技术开始一个新项目。

我已经设法使用nodeexpress 创建了一个RESTful API。输入特定 URL 后,将显示匹配的 Json 响应。到目前为止一切顺利。

现在我正在尝试将 Angular 2 集成到该过程中。我已经创建了 app.component。当页面显示时,组件未加载,我得到了一些 404 代码:

Failed to load resource: 404 (not found) http://localhost:3000/node_modules/core-js/client/shim.min.js
...
Failed to load resource: 404 (not found) http://localhost:3000/systemjs.config.js

这是我的项目结构:

├── App
|    └── app.component.ts                //copied from Angular2/get-started
|    └── main.ts                         // copied from Angular2/get-started
├── dist                                 //contains generated JS files
├── node_modules
├── server                               // contains Server Typescript files
├── index.html                           // copied from Angular2/get-started
├── index.ts                             // server entry point
├── package.json
├── systemjs.config.js
├── tsconfig.json
├── typings.json

我的 package.json:

{
  "main": "index.js",
  "scripts": {
    "start": "tsc && concurrently \"npm run tsc:w\" \"node dist/js/index.js\" ",
    "postinstall": "typings install",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "typings": "typings"
  },
  "dependencies": {

    "@angular/common":  "2.0.0-rc.1",
    "@angular/compiler":  "2.0.0-rc.1",
    "@angular/core":  "2.0.0-rc.1",
    "@angular/http":  "2.0.0-rc.1",
    "@angular/platform-browser":  "2.0.0-rc.1",
    "@angular/platform-browser-dynamic":  "2.0.0-rc.1",
    "@angular/router":  "2.0.0-rc.1",
    "@angular/router-deprecated":  "2.0.0-rc.1",
    "@angular/upgrade":  "2.0.0-rc.1",

    "systemjs": "0.19.27",
    "core-js": "^2.4.0",
    "reflect-metadata": "^0.1.3",
    "rxjs": "5.0.0-beta.6",
    "zone.js": "^0.6.12",
    "angular2-in-memory-web-api": "0.0.11",
    "bootstrap": "^3.3.6",

    "express": "^4.13.4",
    "mysql": "^2.5.4"
  },
  "devDependencies": {
    "concurrently": "^2.0.0",
    "typescript": "^1.8.10",
    "typings": "^1.0.4"
  }
}

还有我的 index.ts(服务器):

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

var __projectRoot = __dirname + '/../../';

app.get('/', function (req, res) {
    res.sendFile(path.join(__projectRoot + '/index.html'));
});

console.log('Server up and running on http://localhost:3000/');
app.listen(server_port);

总之,index.html:

<html>
<head>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <title>Angular 2 QuickStart</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 1. Load libraries -->
    <!-- Polyfill(s) for older browsers -->
    <script src="node_modules/core-js/client/shim.min.js"></script>
    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/reflect-metadata/Reflect.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <!-- 2. Configure SystemJS -->
    <script src="systemjs.config.js"></script>
    <script>
        System.import('app').catch(function (err) {
            console.error(err);
        });
    </script>
</head>
<!-- 3. Display the application -->
<body>
<h1>index.html</h1>
<my-app>Loading...</my-app>
</body>
</html>

加载http://localhost:3000/ 时,我看到“Index.html 正在加载...”,但该组件未显示。

在 Angular2 官方网站上,他们使用 lite-server 依赖项。我猜我的服务器有问题,但我不知道如何让它工作。或者有没有办法使用Expresslite-server实现RESTful API?

【问题讨论】:

    标签: javascript node.js angular express


    【解决方案1】:

    您忘记添加中间件函数来提供静态文件,例如 js libs 和 css。在app.get("/", ...) 之前添加这一行应该可以:

    app.use(express.static(__projectRoot));
    

    【讨论】:

    【解决方案2】:

    安装路径包后 npm i 路径--保存

    在上面使用这个

    var path = require('path');
    并在之后使用它

    var app = express();
     app.use(express.static(path.join(__dirname, 'your-dir-name')));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-04
      • 2019-01-22
      • 2016-11-02
      • 2023-02-18
      • 2020-05-04
      • 2018-02-18
      • 1970-01-01
      • 2018-07-11
      相关资源
      最近更新 更多