Last active
March 22, 2021 22:31
-
-
Save lakesare/350f67ada5680eb6689affe3bb87b962 to your computer and use it in GitHub Desktop.
ES6 fetch + Express 4.14.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import express from 'express'; | |
import bodyParser from 'body-parser'; | |
const app = express(); | |
app.use(bodyParser.json()); // add a middleware (so that express can parse request.body's json) | |
app.post('/api/courses', (request, response) => { | |
response.json(request.body); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch("/api/courses", { | |
method: 'POST', | |
body: JSON.stringify({ hi: 'hello' }), // stringify JSON | |
headers: new Headers({ "Content-Type": "application/json" }) // add headers | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment