2017-08-03 58 views
0

我使用角CLI和的WebPack用以下的package.json配置:快遞路由器不適用於發佈請求?

"dependencies": { 
    "@angular/common": "^4.0.0", 
    "@angular/compiler": "^4.0.0", 
    "@angular/core": "^4.0.0", 
    "@angular/forms": "^4.0.0", 
    "@angular/http": "^4.0.0", 
    "@angular/platform-browser": "^4.0.0", 
    "@angular/platform-browser-dynamic": "^4.0.0", 
    "@angular/router": "^4.0.0", 
    "bcrypt-nodejs": "0.0.3", 
    "body-parser": "^1.17.2", 
    "core-js": "^2.4.1", 
    "express": "^4.15.3", 
    "express-session": "^1.15.4", 
    "mongoose": "^4.11.3", 
    "rxjs": "^5.1.0", 
    "zone.js": "^0.8.4" 
} 

下面是我的根文件夾結構:

enter image description here

我學習意味着贊同Angular2。我在server.js

const express = require('express'); 
const path = require('path'); 
const http = require('http'); 
const bodyParser = require('body-parser'); 
const mongoose = require('mongoose'); 
const session = require('express-session'); 
mongoose.connect("mongodb://localhost/demo"); 

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

const app = express(); 
app.use(session({ 
    secret: 'somesecrettokenhere', 
    name: 'demo', 
    resave: false, 
    saveUninitialized: true 
})); 

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


app.get('/', (req, res) => { 
    res.sendFile(path.join(__dirname, '../dist/index.html')); 
}); 

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

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

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

/** 
    * Get port from environment and store in Express. 
*/ 
const port = process.env.PORT || '3000'; 
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}`)); 

下面下面的代碼是我api.js:

const express = require('express'); 
const router = express.Router(); 

router.post('/login', (req, res) => { 
    console.log('Run'); 
}); 

module.exports = router; 

下面是我的Angular2 service.ts:

import { Injectable } from '@angular/core'; 
import {Http, Response, Headers} from '@angular/http'; 
import { Observable } from 'rxjs'; 
import 'rxjs/add/operator/map'; 

@Injectable() 
export class AuthService { 

    constructor(private _http: Http) { } 

     Login(email: string, password: string): Observable<Response> { 
     let credentials = { 
     _email: email, 
     _password: password 
     }; 
     return this._http.post('/api/login',{_uc:credentials}).map(
      (res: Response) => res.json() 
    ); 
    } 

} 

問題:

當我點擊我的表單提交按鈕它擊中登錄方法是o ñ我的service.ts文件,但不顯示任何錯誤在服務器端控制檯或沒有擊中我的服務器api.js登錄後的方法。我不知道爲什麼我的服務器端api.js登錄後的方法沒有打,並沒有在控制檯窗口中顯示任何錯誤。請告訴我我的代碼中做錯了什麼。客戶端代碼是工作文件我把控制檯放在我的service.ts登錄方法中,它顯示我的輸出,但http請求在服務器端不起作用。請讓我知道是否有任何其他細節需要在我的問題。

回答

2

當我點擊我的形式提交按鈕它擊中的登錄方法,它是對我的service.ts

您需要申請可觀察到的,以「火」了。

實施例:this.authService.Login(..).subscribe((res)=> console.log(res));

+0

你說得對,我忘記訂閱我的登錄方法,這就是爲什麼我沒有得到404的任何錯誤。 –

+1

@AhmerAliAhsan和mscdex回答,你的路徑是錯誤的:-)但它沒有吸引我的眼睛:-) – echonax

+0

你是對的我的朋友。這是我的第一個基本錯誤:-) –

2

你安裝在 '/路由'(app.use('/routes', api);)的基本URL api.js。所以你可以通過'/ routes/login'來訪問它。

如果這不是你想要的,那麼就像你的客戶端代碼所期望的那樣將它掛載到'/ api'。

+0

plus1提到我的第一個基本錯誤。 –

2

我認爲這是從你身邊一個錯誤,你敲錯一些如何

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

嘗試將其更改爲

app.use('/api', api); 

將是肯定的工作,如果你是外出理想的大項目方式路線應該是不同的文件夾。

就需要必要的文件

server.js

如果你想修改路線命名約定看看express-namespace

+0

mscdex和echonax的答案解決了我的問題。欣賞你的答案。 –