L
L
lavezzi12018-09-30 12:45:53
Express.js
lavezzi1, 2018-09-30 12:45:53

How to save a static page at the root using express?

Now there is such a code, the index page throws a 404 component from the react rotator. How to rewrite the router? I want a static landing on / index. The rest is react. Or maybe something else is needed?

app.use(express.static(path.join(__dirname, '../build')));

// secure your private routes with jwt authentication middleware
app.all('/api/v1/*', (req, res, next) => auth(req, res, next));

// Handle all v1 routes
fs.readdirSync(`${__dirname}/routes/v1`).forEach((file) => {
  require(`./routes/v1/${file}`)(app);
});

app.get('/', (req, res) => {
  res.sendFile(path.resolve(__dirname, '../build/landing/index.html'));
});

app.all('*', (req, res) => res.status(404).json({
  message: "Seems like the endpoint you're looking for no longer exists ",
}));

app.get('*', (req, res) => {
  res.sendFile(path.resolve(__dirname, '../build/index.html'));
});

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question