Nodejs express 获取url参数,post参数的三种方式

2017-03-20 17:31:55

Js代码  收藏代码

  1. express获取参数有三种方法:官网实例:  

  2.   

  3. Checks route params (req.params), ex: /user/:id  

  4. Checks query string params (req.query), ex: ?id=12  

  5. Checks urlencoded body params (req.body), ex: id=  

 

1、例如:127.0.0.1:3000/index,这种情况下,我们为了得到index,我们可以通过使用req.params得到,通过这种方法我们就可以很好的处理Node中的路由处理问题,同时利用这点可以非常方便的实现MVC模式;

 

2、例如:127.0.0.1:3000/index?id=12,这种情况下,这种方式是获取客户端get方式传递过来的值,通过使用req.query.id就可以获得,类似于PHP的get方法;

3、例如:127.0.0.1:300/index,然后post了一个id=2的值,这种方式是获取客户端post过来的数据,可以通过req.body.id获取,类似于PHP的post方法;

 

注:post请求需要

Js代码  收藏代码

  1. var express        =         require("express");  

  2. var bodyParser     =         require("body-parser");  

  3. var app            =         express();  

  4.   

  5. // need it...  

  6. app.use(bodyParser.urlencoded({ extended: false }));  

  7.   

  8. app.post('/login',function(req,res){  

  9.   var user_name=req.body.user;  

  10.   var password=req.body.password;  

  11.   console.log("User name = "+user_name+", password is "+password);  

  12.   res.end("yes");  

  13. });  


  • 2017-11-10 00:06:15

    CORS: credentials mode is 'include'

    XMLHttpRequest cannot load http://localhost/Foo.API/token. The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:5000' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

  • 2017-11-19 00:17:51

    Java如何获取Date的“昨天”与“明天”示例代码

    最近在做项目的时候用到Date和Calendar比较多,而且用到的方式也比较全,突然想到一个问题,Java如何获取Date的"昨天"与"明天",也就是前一天和后一天呢?思考后写出了方法,想着万一以后用到,就总结出来,也方便有需要的朋友们参考借鉴,下面来一起看看吧。