I just got into Next.js and I cannot figure out how to connect my next.js app with my express api. I was able to make it work but I'm pretty sure this is not the right way to implement it since in my Index component I hard coded the fetch url and I know that in production if the port is not the same it won't work.
I tried to put only the route and the fetch API doesn't allow me to do so.
My index page looks like so
import Link from "next/link";
import fetch from "isomorphic-unfetch";
const Index = props => (
<>
<h1>Youtubers</h1>
<ul>
{props.youtubers.map(youtuber => (
<li key={youtuber._id}>
<Link as={`/p/${youtuber._id}`} href={`/post?id=${youtuber.id}`}>
<a>{youtuber.name}</a>
</Link>
</li>
))}
</ul>
</>
);
Index.getInitialProps = async function() {
// this url I think is wrong ↓
const res = await fetch("https://localhost:5000/youtuber");
const data = await res.json();
return {
youtubers: data.youtubers
};
};
export default Index;
And in my server.js I have this inside the app.prepare().then()
server.use(bodyParser.urlencoded({ extended: false }));
server.use(bodyParser.json());
mongoose.connect(process.env.MONGODB_PASSWORD, {
useNewUrlParser: true,
useCreateIndex: true
});
mongoose.connection.on("open", function() {
console.log("mongodb is connected!!");
});
mongoose.connection.on(
"error",
console.error.bind(console, "MongoDB connection error:")
);
//CORS handler
server.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
if (req.method === "OPTIONS") {
res.header(
"Access-Control-Allow-Methods",
"PUT, POST, PATCH, DELETE, GET"
);
return res.status(200).json({});
}
next();
});
//Question Route
server.use("/youtuber", youtuberRoutes);
server.get("*", (req, res) => {
return handle(req, res);
});
server.listen(port, err => {
if (err) throw err;
console.log(`> Ready on http://localhost:${port}`);
});
fetchis usinghttps, but I don't seehttpsbeing set up in your Express app. Without seeing howyoutuberRoutesis defined, it's not possible to see whether the URL you're matching is going to match any routes.