Skip to content

Instantly share code, notes, and snippets.

@lakesare
Last active March 22, 2021 22:31
Show Gist options
  • Save lakesare/350f67ada5680eb6689affe3bb87b962 to your computer and use it in GitHub Desktop.
Save lakesare/350f67ada5680eb6689affe3bb87b962 to your computer and use it in GitHub Desktop.
ES6 fetch + Express 4.14.0
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);
});
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