Error: ENOENT: no such file or directory, stat 'C: \ public \ index.html'

3

Trying to visualize the main html file, I could not find the specific path, no matter how hard I tried.

server.js

import express from 'express';
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackConfig from '../webpack.config';

const app = express();

app.set('port', process.env.PORT || 3000);

app.use(webpackDevMiddleware(webpack(webpackConfig)));

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

app.get('/api', (req, res) => {
    res.json({api: "Woks Fine"});
});

app.listen(app.get('port'), () => {
    console.log("App Start in Port", app.get('port'));
});

export default app;

Project structure

    
asked by Pedro Miguel Pimienta Morales 26.03.2018 в 23:52
source

1 answer

2

The route should be:

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

Remember to prepend the point, because by not doing so you are saying that index.html is in the absolute path /public/index.html and not in the relative path ./ public / index.html

    
answered by 03.04.2018 в 23:23