mercredi 21 octobre 2020

Express Post Does Not Answer The Client Side

I'm new to web dev and I am trying to send some data using post from the client side to the server side, but the post method wouldn't answer

here is my server

const express = require('express')
const server = express()
let path = require('path')
let fs = require('fs')
const { request } = require('http')

server.get('/',(req,res)=>{
    res.sendFile(path.join(__dirname + '/public/welcome.html'))
})
server.use('/',express.static(__dirname+'/public'))

server.post('/', (req,res)=>{
    console.log(req.url);
    console.log(req.body);
    res.send('test')

})



server.listen(3000,()=>{
    console.log('listened')
    console.log(__dirname)
})

and here is my client side

function signIn(){
        var xhr = new XMLHttpRequest();
        var url = "/singIn";
        xhr.open("POST", url, true);
        xhr.setRequestHeader("Content-Type", "application/json");
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                alert(xhr.responseText);
                // var json = JSON.parse(xhr.responseText);
            }
        };
        let account = document.getElementById('Account').value;
        let passwd = document.getElementById('pwd').value;
        var data = JSON.stringify({"email": account, "password": passwd});
        xhr.send(data);

    }

Aucun commentaire:

Enregistrer un commentaire