简体   繁体   English

NodeJs路由目录与Angular 2 rc.5

[英]NodeJs routing directory with Angular 2 rc.5

I'm working on my first a NodeJs + Angular 2 project. 我正在研究我的第一个NodeJs + Angular 2项目。 It worked perfectly fine without Angular2 in my previous projects with these kind of routes. 在我以前使用这些路线的项目中,没有Angular2,它完美无缺。

I got error on accessing multi-level directory routing in NodeJs. 我在NodeJs中访问多级目录路由时遇到错误。

Example: 例:

It works in this way: 它以这种方式工作:

app.get('/dashboard', dashboard.getHome);        // works
app.get('/user', user.getUser);                  // works

Instead of: 代替:

app.get('/dashboard/home', dashboard.getHome);   // won't work
app.get('/user/home', user.getUser);             // won't work

I understand there's Angular2 routing to work with but is there anyway to directly access to the static URL which I specified in NodeJs? 我知道有Angular2路由可以使用,但是无论如何直接访问我在NodeJs中指定的静态URL?

I'll get error if I wanna access more than 1 level like : 如果我想访问超过1级,我会收到错误:

/dashboard/tasks/42

I'm not able to do it now and that's the problem I'm struggling with. 我现在无法做到这一点,这就是我正在努力解决的问题。

app.js app.js

var main = require('./routes/index');
var dashboard = require('./routes/dashboard');
var user = require('./routes/user');

app.get('/', main.index);

app.get('/dashboard', dashboard.getHome);        // works
// app.get('/dashboard/home', dashboard.getHome);   // won't work this way

app.get('/user', user.getUser);                  // works
// app.get('/user/home', user.getUser);             // won't work this way

dashboard/index.html 仪表板/ index.html的

<html>
<head>
    <title>Teamo - Dashboard</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="/css/style.css">
    <!-- 1. Load libraries -->
    <!-- Polyfill(s) for older browsers -->
    <script src="javascripts/core-js/client/shim.min.js"></script>
    <script src="javascripts/zone.js/dist/zone.js"></script>
    <script src="javascripts/reflect-metadata/Reflect.js"></script>
    <script src="javascripts/systemjs/dist/system.src.js"></script>
    <!-- 2. Configure SystemJS -->
    <script src="systemjs.config.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.8/socket.io.min.js"></script> 
    <script>
    System.import('app/dashboard/dashboard').catch(function(err){ console.error(err); });
    </script>
</head>
<!-- 3. Display the application -->
<body>
    <my-dashboard>Loading...</my-dashboard>
</body>

It won't work using this URL even if I've set the route below in app.js: 即使我在app.js中设置了以下路由,它也无法使用此URL:

localhost:3000/dashboard/home

It returns the errors below. 它返回以下错误。

Error: 错误:

GET http://localhost:3000/dashboard/javascripts/core-js/client/shim.min.js 
GET http://localhost:3000/dashboard/javascripts/zone.js/dist/zone.js 
GET http://localhost:3000/dashboard/javascripts/reflect-metadata/Reflect.js 
GET http://localhost:3000/dashboard/javascripts/systemjs/dist/system.src.js 
GET http://localhost:3000/dashboard/systemjs.config.js 
GET http://localhost:3000/dashboard/js/pace.min.js 
Uncaught ReferenceError: System is not defined(anonymous function) @ home:18
GET http://localhost:3000/dashboard/js/pace.min.js 
favicon.ico:1 GET http://localhost:3000/favicon.ico 500 (Internal Server Error)

Change the order of the elements: 更改元素的顺序:

 app.get('/dashboard', dashboard.getHome);        // works
 app.get('/dashboard/home', dashboard.getHome);   // won't work

to: 至:

   app.get('/dashboard/home', dashboard.getHome);   // won't work
   app.get('/dashboard', dashboard.getHome);        // works

Express is sensitive to the order. Express对订单很敏感。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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