客户端GET设置参数查询:

search() {
const params = new HttpParams()
.set('userName', this.userName)
.set('fullName', this.fullName);
this.http.get('/api/users', {params})
.subscribe(data => {
this.users = data['users'];
});
}
reset() {
this.userName = '';
this.fullName = '';
this.search();
}

node服务器响应:

app.get('/users', (req, res) => {
if (_.isEmpty(req.query.userName)&&_.isEmpty(req.query.fullName)) {
res.json({users: users});
} else {
res.json({
users: users.filter(
(user) =>
((user.userName).indexOf(req.query.userName)) > 0 ||
((user.fullName).indexOf(req.query.fullName)) > 0
)
});
}
});

跨域设置:

app.all('*', function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By", ' 3.2.1');
res.header("Content-Type", "application/json;charset=utf-8");
next();
});

bodyParse use:

var express = require('express')
var bodyParser = require('body-parser')
var app = express()
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))//extended为false表示使用querystring来解析数据,这是URL-encoded解析器
// parse application/json
app.use(bodyParser.json())//添加json解析器
app.use(function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.write('you posted:\n')
res.end(JSON.stringify(req.body, null, 2))
})
var jsonParser = bodyParser.json()//获取JSON解析器中间件
// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })//url-encoded解析器
// POST /login gets urlencoded bodies
app.post('/login', urlencodedParser, function (req, res) {//注册URL解析器
if (!req.body) return res.sendStatus(400)
res.send('welcome, ' + req.body.username)
})
// POST /api/users gets JSON bodies
app.post('/api/users', jsonParser, function (req, res) {//使用json中间件获取json数据
if (!req.body) return res.sendStatus(400)
// create user in req.body
})
// parse various different custom JSON types as JSON
app.use(bodyParser.json({ type: 'application/*+json' }))
// parse some custom thing into a Buffer
app.use(bodyParser.raw({ type: 'application/vnd.custom-type' }))
// parse an HTML body into a string
app.use(bodyParser.text({ type: 'text/html' }))

GET,POST,DELTE,PUT:

app.get('/users', function (req, res) {
if (_.isEmpty(req.query.userName) && _.isEmpty(req.query.fullName)) {
res.json({users: users});
}
else {
res.json({
users: users.filter(function (user) {
return ((user.userName).indexOf(req.query.userName)) > 0 ||
((user.fullName).indexOf(req.query.fullName)) > 0;
})
});
}
});
app.post('/users', jsonParser, urlencodedParser, function (req, res) {
var user = req.body.user;
users.push(user);
res.send('save success');
}); app.delete('/users/:id', function (req, res) {
const deleteUser = users.findIndex(function (user) {
return user.id === req.params.id;
});
users.splice(deleteUser, 1);
res.send('delete success');
});
app.put('/api/tour/:id',function (req,res) {
var id = req.params.id;
var p = tours.some(function (p) {
return p.id == id;
});
if(p){
if(req.query.name) tours[id].name = req.query.name;
if(req.query.price) tours[id].price = req.query.price;
res.json({success:true,tours:tours,p:p});
}else{
res.json({error:'No such tour exists.'});
}
});

@angular/cli项目构建--http(2)的更多相关文章

  1. @angular/cli项目构建--组件

    环境:nodeJS,git,angular/cli npm install -g cnpm --registry=https://registry.npm.taobao.org cnpm instal ...

  2. @angular/cli项目构建--modal

    环境准备: cnpm install ngx-bootstrap-modal --save-dev impoerts: [BootstrapModalModule.forRoot({container ...

  3. @angular/cli项目构建--Dynamic.Form

    导入所需模块: ReactiveFormsModule DynamicFormComponent.html <div [formGroup]="form"> <l ...

  4. @angular/cli项目构建--animations

    使用方法一(文件形式定义): animations.ts import { animate, AnimationEntryMetadata, state, style, transition, tri ...

  5. @angular/cli项目构建--interceptor

    JWTInterceptor import {Injectable} from '@angular/core'; import {HttpEvent, HttpHandler, HttpInterce ...

  6. @angular/cli项目构建--路由3

    路由定位: modifyUser(user) { this.router.navigate(['/auction/users', user.id]); } 路由定义: {path: 'users/:i ...

  7. @angular/cli项目构建--httpClient

    app.module.ts update imports: [ HttpClientModule] product.component.ts import {Component, OnInit} fr ...

  8. @angular/cli项目构建--路由2

    app.module.ts update const routes: Routes = [ {path: '', redirectTo: '/home', pathMatch: 'full'}, {p ...

  9. @angular/cli项目构建--路由1

    app.module.ts import {BrowserModule} from '@angular/platform-browser'; import {NgModule} from '@angu ...

  10. @angular/cli项目构建--Dynamic.Form(2)

    form-item-control.service.ts update @Injectable() export class FormItemControlService { constructor( ...

随机推荐

  1. 《Python 数据分析》笔记——pandas

    Pandas pandas是一个流行的开源Python项目,其名称取panel data(面板数据)与Python data analysis(Python 数据分析)之意. pandas有两个重要的 ...

  2. 字符串之strchr

    功能:查找字符在字符串中第一次出现的位置. #include <iostream> #include <assert.h> using namespace std; char ...

  3. Kafka高可用的保证

    zookeeper作为去中心化的集群模式,消费者需要知道现在那些生产者(对于消费者而言,kafka就是生产者)是可用的.    如果没有zookeeper每次消费者在消费之前都去尝试连接生产者测试下是 ...

  4. HDU 3199 Hamming Problem

    Hamming Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  5. Video标签的使用

    现在如果要在页面中使用video标签,需要考虑三种情况,支持Ogg Theora或者VP8(如果这玩意儿没出事的话)的(Opera.Mozilla.Chrome),支持H.264的(Safari.IE ...

  6. position:absolute width

    position:absolute; left:0px; right:0px; top:0px; bottom:0px; 设置布满整个父范围,设置了absolute的元素可以通过以上4个属性来展开面, ...

  7. SPOJ - HORRIBLE 【线段树】

    思路 线段树 区间更新 模板题 注意数据范围 AC代码 #include <cstdio> #include <cstring> #include <ctype.h> ...

  8. springboot获取URL请求参数的几种方法

    原文地址:http://www.cnblogs.com/xiaoxi/p/5695783.html 1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于pos ...

  9. 【HackerRank】 Sherlock and The Beast

    Sherlock and The Beast Sherlock Holmes is getting paranoid about Professor Moriarty, his archenemy. ...

  10. FullPage.js全屏滚动插件

    一.介绍 fullPage.js是一个基于jQuery的插件,他能够很方便.很轻松的制作出全屏网站,主要功能有: 1.支持鼠标滚动 2.多个回调函数 3.支持手机.平板触摸事件 4.支持CSS3动画 ...