Skip to content
Discussion options

You must be logged in to vote

const express = require("express");
const axios = require("axios");

const app = express();
const PORT = 3000;

// Route that connects to an external API
app.get("/users", async (req, res) => {
try {
const response = await axios.get("https://jsonplaceholder.typicode.com/users");
res.json(response.data);
} catch (error) {
res.status(500).json({ message: "Error fetching API data" });
}
});

app.listen(PORT, () => {
console.log(Server running on port ${PORT});
});

  1. Run the Server
    node server.js

Then open:

http://localhost:3000/users

Your Node server connects to the API and returns the data.

  1. Alternative Using Native fetch (Node 18+)
    const express = require("express");

const app = express();

a…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Sengwamana
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants