HTTP Request JavaScript

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' }

--

--

--

Learning javascript and web-development

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What is package.json ?

Better Gatsby + Typescript Setup

Neumorphic Login Form In Bulma

JetBrains Projector : Bringing New Life to Legacy Java Swing Applications

Learning JavaScript in 2020

JavaScript Design Patterns: The Adapter Pattern

Ridiculously Funny Programming Memes to Make Every Developer Laugh Out Loud

ServiceNow Webhooks

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
Cho Zin Thet

Cho Zin Thet

Learning javascript and web-development

More from Medium

How to Setup A BasicExpress Server From Scratch

a picture of package.json

Automatic Deployment with GitHub on Any Server Instance without GitHub workflow or SSH

Building Web Service with NodeJS — Part 2 (Routing)