我的页面的Angular 5 POST请求 [英] Angular 5 POST requests for my page
问题描述
我正在接收到Angular页面的POST请求,该请求可能如下:
I'm receiving POST request to my Angular page, which might look like:
https://www.myangularsite.com/login
"POST /login HTTP/1.1
token: randomstring"
我的应用模块看起来像
export const routes: Routes = [
{ path: 'login', component: LoginComponent },
{ path: '', redirectTo: 'login', pathMatch: 'full' },
{ path: '**', redirectTo: 'login', pathMatch: 'full' }
];
@NgModule({
declarations: [
LoginComponent,
AppComponent
],
imports: [
BrowserModule,
HttpClientModule,
RouterModule.forRoot(routes, {errorHandler: errorLogger } )
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
我的问题是Angular应用程序不接受POST方法请求,仅接受GET. 页面显示
My problem is that Angular application doesn't accept POST method requests, only GET. Page displays
Cannot POST /login
我正在通过 ng服务运行Web服务器.是否可以将其设置为侦听POST请求?我无法将收到的请求更改为GET.
I'm running web server through ng serve. Is it possible to set it up to listen to POST requests? I can't change received request to GET.
即使是POST请求,也不是后端调用.授权后调用(类似于oauth),因此登录后应显示页面.
Even though it is POST request, it is not a backend call. It is called after authorization (similar from oauth), therefore it should display page after login.
推荐答案
You can use node.js Express as your CLI server. Please see the post at http://tattoocoder.com/angular2-giving-your-cli-server/ on how to do that.
然后在您的express server.js中,您可以侦听登录请求:
Then in your express server.js, you can listen the post request for login:
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get('/',function(req,res){
res.sendfile("index.html");
});
app.post('/login',function(req,res){
var user_name=req.body.user;
var password=req.body.password;
console.log("User name = "+user_name+", password is "+password);
res.end("yes");
});
app.listen(3000,function(){
console.log("Started on PORT 3000");
})
这篇关于我的页面的Angular 5 POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!