2014-12-16 4 views
1

사용자가 비밀번호를 재설정 할 수있는 스크립트를 작성 중입니다. 사용자가 해당 링크를 클릭 할 때nodejs의 비밀번호 재설정

사용자

My example

같은 이메일을 받게됩니다. 양식이 으로 열리고 사용자가 새 암호를 입력 할 수 있습니다. 이 내 경로 코드

app.get('/resetpassword/:token', function (req, res, next) { 
     var fileLocation = path.resolve(__dirname + '/../public/resetpassword.html'); 
     console.log(fileLocation); 
     res.sendfile(fileLocation); 
    }); 

    app.post('/setnewpassword', function(req,res,next){ 
       console.dir(req.body); 
    }); 

하고 내 HTML은

<form action="/setnewpassword" method="POST"> 
     <div> 
      <label>Password:</label> 
      <input type="password" name="password" /> 
     </div> 
     <input type="submit" value="Submit" /> 
    </form> 

이제 문제는 내가 (app.post에서 '/ setnewpassword'

어떤을 PARAMS을받지 오전이있다 아이디어? setnewpassword에서 매개 변수를 얻는 방법

POST 메서드 만 사용하고 싶습니다.

+0

당신이 구문 분석 bodyparser를 사용에 POST PARAMS는? – aega

+0

그것을 사용하는 방법. 난 아무 생각이 –

+0

더 아부를 알려 귀하의 코드, 당신은 표현을 사용하고 있습니까? 브라우저에서 요청을 확인 했습니까? 그것의 params 일부입니까? 예인 경우 req의 헤더는 무엇입니까? 응용 프로그램/json입니까? – 1Mayur

답변

0
var bodyParser = require('body-parser') 
// parse application/x-www-form-urlencoded 
app.use(bodyParser.urlencoded({ extended: false })) 
// parse application/json 
app.use(bodyParser.json()) 

포스트 핸들러 앞에 코드를 삽입해야합니다.

+0

작동하지 않는다. 이 코드를 사용합니다. app.use (bodyParser); \t app.get ('/ resetpassword/: 토큰'다음, 기능 (REQ, 입술) { \t \t VAR의 fileLocation = path.resolve (__있는 dirname + '/../public/resetpassword.html'); \t \t console.log (fileLocation); \t \t res.sendfile (fileLocation); \t}); \t \t app.post (다음 '/ setnewpassword'기능 (REQ, 입술) { \t \t \t \t console.dir (req.params) \t}); –

+0

가 코드를 업데이트했습니다. 지금 해봐. – aega

+0

빈 매개 변수를 가져 오는 중 –

0

먼저 당신의 app.js 처리기에서 다음

var bodyParser = require('body-parser'); 
..... 
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({extended: false})); 

에 bodyparser 미들웨어를 추가,이 같은 접근 양식 매개 변수 : 당신이 REST 클라이언트를 사용하는 경우

var password = req.params.password || ''; 

, 컨텐츠 유형을하는 설정 application/x-www-form-urlencoded입니다.

0

당신은

app.get('/resetpassword/:token', function (req, res, next) 

하지만 PARAMS 가능 "당신이 app.post ('/ setnewpassword'에 PARAMS을 받고되지 않습니다"당신이 HTML 페이지에서 다음이 페이지를 HTML로 PARAM과를 보낼 필요가 말했다

app.post('/setnewpassword/:token',function(req,res){ 
}); 
+0

날 어떻게 HTML 페이지에 param을 보내려면 –

+0

예. resetpassword에 params를 얻고 있습니다. –

+0

이제/setnewpassword API 호출에 전달해야합니다. – Abhimanyu

0
app.post('/changepass' , function (req, res, next) { 
    if (newpass !== newpassconfirm) { 
     throw new Error('password and confirm password do not match'); 
    } 

    var user = req.user; 

    user.pass = newpass; 

    user.save(function(err){ 
     if (err) { next(err) } 
     else { 
      res.redirect('/account'); 
     } 
    }) 
});