2017-02-22 74 views
0

我正在使用expressjs,PHP,HTML和MongoDB快遞js和PHP函數

我在 「的index.html」 文件有這樣的代碼

<form action="/Login" method="POST"> 
    <input type="text" placeholder="name" name="Username"> 
    <input type="Password" placeholder="Password" name="Password"> 
    <button type="submit">Submit</button> 
</form> 

我有這樣的一個項目在我的「Server.js」文件(Expressjs)代碼

app.post('/Login', (req, res) => { 
    //to know that I really had recieve a data from my index.html 
    console.log(req.body) 
    //to collect data from my database (MongoDB) 
    db.collection('User').find(req.body,{Username:"",password:""}).toArray(function(err, results) { 
    //show result from the database 
    console.log(results) 

    //The Question Starts here. 
    }) 
}) 

我怎麼能同時發送這些數據(req.body和結果)對我的PHP文件?所以我可以驗證它。 以及如何將數據從PhP發送回服務器,以便服務器可以將數據發送回客戶端?

+0

只是'res.write()'和'res.end()'? –

+0

您是否試圖向PHP服務器發出請求?或者運行一個PHP腳本?目前還不清楚PHP如何適應這一點。 – jfriend00

+0

運行一個不同文件的腳本讓我們把它叫做「login.php」 –

回答

0

您可以使用fetch將請求發佈到php服務器。示例來自文檔:

fetch('flowers.jpg') 
.then(function(response) { 
    return response.blob(); 
}) 
.then(function(myBlob) { 
    var objectURL = URL.createObjectURL(myBlob); 
    myImage.src = objectURL; 
});