學習實踐之DEMO《nodejs模擬POST登錄》

一個簡單的PHP接收參數頁面
<?php
    header("content-type:text/html;charset=utf-8");
    if($_POST[username] == "admin" && $_POST[password] == "123456") {
        echo "登錄成功";
    }else {
        echo "登錄失敗";
    }
?>
nodejs代碼實現模擬登錄示例
var http = require("http");
var querystring = require("querystring"); 
var contents = querystring.stringify({
    username: 'admin', password: '123456' });
var options = {
    hostname: 'localhost',
    port: 9000,
    path: '/modules/login/checkSign.php',
    method: 'POST',
    headers:{
        "Content-Length":contents.length,
        "Content-Type":"application/x-www-form-urlencoded"
  } };

var req = http.request(options, function(res) { res.setEncoding('utf8'); res.on('data', function (data) { console.log('BODY:' + data); }); }); req.on('error', function(e) { console.log('problem with request: ' + e.message); }); req.write(contents); req.end();
相關文章
相關標籤/搜索