HTTP Request JavaScript

Cho Zin Thet
1 min readMar 12, 2021

NodeJS

There is two routes as below in my nodeJS server.

app.get('/get', (req, res) => {
res.json({name: 'Jack', age: '22'});
})
app.post('/post', (req, res) => {
console.log(req.body);
})

GET Request to Node Server (my URL > localhost:9000)

  • fetch method

fetch method is used to make web requests to server.

fetch('http://localhost:9000/get')
.then(response => response.json())
.then(json => console.log(json))
axios.get('http://localhost:9000/get')
.then(response => response.data)
.then(data => console.log(data));

And you will see in your browser console

{name: "Jack", age: "22"}
age: "22"
name: "Jack"

POST Request to Node Server (my URL > localhost:9000)

  • fetch Method

fetch method is used to make web requests to server.

fetch("http://localhost:9000/post", {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({username: 'abcd', email: 'abcd123@gmail.com'})
}).then(res => console.log(res));
axios.post('http://localhost:9000/post', {
username: 'abcd',
email: 'abcd123@gmail.com'
})
.then(response => console.log(response))

server will receive like this

{ username: 'abcd', email: 'abcd123@gmail.com' }

--

--