Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 72416

How to catch POST request on back-end?

$
0
0
<!doctype html>
<head>
</head>

<body>
<script>
    const Http = new XMLHttpRequest();
    const url='http://localhost:4550/users';
    Http.open("POST", url);
    Http.send("hey");

    Http.onreadystatechange = (e) => {
        console.log(Http.responseText)
    }
</script>

</body>
</html>

//user.js
var express = require('express');
var router = express.Router();

var array = [];

/* GET users listing. */
router.get('/', (req, res, next) => {
  res.send('respond with a resource1');
});

router.post('/', (req, res, next) => {
  res.send('respond with a resource2');
});

module.exports = router;

//app.js
const express = require('express')

const app = express();

app.get('/',(req,res)=> {
  console.log('lior');
  res.send('api running 2')});

app.use('/users',require('./routes/users'))

app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

const PORT = process.env.PORT || 4550;

app.listen(PORT,()=> console.log('server started on port ${PORT}'));

I am new with connecting client and server side, and it might be why I couldn't find an answer for my question. Is a simple question.

As you can see I want to send "hey" from the client to the server(user.js). However I don't know how does I catch the response on the server side.

I know that a "hey" or neither the code make much sense, but is just an example to make things simple, I just wondering how does the server side could catch and handle the data.

Thanks in advance!


Viewing all articles
Browse latest Browse all 72416

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>