Using Express js as a static server
Using express js as a static server to serve react or next js build.
Intro
A Simple code snippet to server static builds from views directory, from an express server.
This can be used to serve react js and next js apps.
Put your static content in a directory named
static.
npm init --yes
npm i expressconst express = require("express");
const app = express();
app.use("/", express.static(__dirname + "/static"));
app.get("/", (req, res) => {
res.sendFile("views/index.html", { root: __dirname });
});
app.get("*", (req, res) => {
res.sendFile("views/404.html", { root: __dirname });
});
const PORT = process.env.PORT || 8000;
app.listen(PORT, () => console.log(`Started At ${PORT}`));