本文介绍了运行Angular2应用程序与Node Js服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm starting a new project with Angular2 (client-side) and Node JS (server-side) technologies.

我已经设法创建了一个RESTful API使用节点 express 。当输入特定的URL时,将显示匹配的Json响应。到目前为止这么好。

I've managed to create a RESTful API using node and express. When specific URL is entered, the matching Json response is displayed. So far so good.

现在我正在整合Angular 2。我创建了app.component。当页面显示时,组件未加载,我有一些404代码:

Now I'm trying to integrate Angular 2 in the process. I've created the app.component. When the page is displayed, the component is not loaded and I got some 404 codes:

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

这是我的项目结构: / p>

Here is my project structure:

├── 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:

My 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(服务器):

And my index.ts (server):

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:

To conclude, 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>

加载,我看到Index.html加载...,但不显示该组件。

When loading http://localhost:3000/, I see "Index.html Loading..." but the component is not displayed.

在官方Angular2网站上,他们使用 lite-server 依赖关系。我想我的服务器有问题,但是我无法弄明白如何使其工作。或者有没有办法使用 Express lite-server 实现RESTful API?

On the official Angular2 website, they use lite-server dependency. I guess my server has something wrong, but I can't figure out how to make it work. Or is there a way to implement RESTful API using Express and lite-server?

推荐答案

你忘了添加一个中间件函数来提供像js libs和css这样的静态文件。在 app.get(/,...)之前添加此行应该可以工作:

You forgot to add a middleware function to serve static files like js libs and css. Adding this line before app.get("/", ...) should work:

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

这篇关于运行Angular2应用程序与Node Js服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 06:45