D
D
Denis Bukreev2018-08-30 19:05:59
Express.js
Denis Bukreev, 2018-08-30 19:05:59

How to hang the opening of one file on all routes?

This is the code that doesn't work:

app.get('*', express.static(path.join(__dirname, '../public')))

public/index.html is still opened only on /, otherwise:
5b88168d5e737011584224.png
And it is necessary that public/index.html be opened on any get-request in general, excluding the prefix /api/.
How can this be achieved?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-08-30
@denisbookreev

const express = require('express');
const path = require('path');

const app = express();

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

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question