我使用角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"
}
下面是我的根文件夾結構:
我學習意味着贊同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請求在服務器端不起作用。請讓我知道是否有任何其他細節需要在我的問題。
你說得對,我忘記訂閱我的登錄方法,這就是爲什麼我沒有得到404的任何錯誤。 –
@AhmerAliAhsan和mscdex回答,你的路徑是錯誤的:-)但它沒有吸引我的眼睛:-) – echonax
你是對的我的朋友。這是我的第一個基本錯誤:-) –