Skip to content

Instantly share code, notes, and snippets.

@charlesroper
Last active February 17, 2021 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charlesroper/ecc6ab9103ba723cb0028d86e5ba895d to your computer and use it in GitHub Desktop.
Save charlesroper/ecc6ab9103ba723cb0028d86e5ba895d to your computer and use it in GitHub Desktop.
const express = require("express");
const fetch = require("node-fetch");
const cors = require("cors");
const app = express();
const NYT = {
endpoint: "https://api.nytimes.com/svc/topstories/v2/",
apiKey: process.env.NYT_API_KEY
};
app.use(cors());
app.get("/topstories/:section", (request, response) => {
const section = request.params.section;
const URL = `${NYT.endpoint + section}.json?api-key=${NYT.apiKey}`;
fetch(URL)
.then(response => response.json())
.then(json => response.send(json))
.catch(console.error("Something went wrong"));
});
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment