HTTP Request JavaScript

NodeJS

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('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));
{name: "Jack", age: "22"}
age: "22"
name: "Jack"

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

  • fetch Method
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))
{ username: 'abcd', email: 'abcd123@gmail.com' }

--

--

Learning javascript and web-development

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store