Serving REACT component using express is not working (just html file is rendered)

Rachomir

Hello I am learning MERN stack and I have problem with serving my react component this is server.js code

const path = require('path')
const fs = require('fs')
const express = require('express')
const app = express()

app.set('view engine', 'html')
app.engine('html', function (path, options, callback) {
  fs.readFile(path, 'utf-B', callback)
})

const cors = require('cors')
app.use(cors({
  'origin': '*',
  'methods': 'GET,HEAD,PUT,PATCH,POST,DELETE',
  'preflightContinue': false,
  'optionsSuccessStatus': 204
}))

const bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))

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

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

app.use('/*', (req, res) => res.status(404).send())

const isDev = true
app.use((err, req, res, next) => {
  res.status(500).json({
    error: 500,
    message: 'Internal Server Error!',
    stack: isDev
      ? err.stack
      : null
  })
})

const port = 8080
app.listen(port, () => {
  console.log('app running at localhost:' + port)
})

Folder structure

build
  bundle.js
  index.html
client
  App.js
  index.html
  index.js
server
  server.js
webpack.config.js
package.json

There is no issues viisble in console - HTML is rendering properly however React component is not visible.

this is my index.html file:

 <!doctype html>
<html lang='en'>
<head>
  <meta charset='utf-8'>
  <meta http-equiv='x-ua-compatible' content='IE=edge'>
  <meta name='viewport' content='width=device-width, initial-scale=1, shrink-to-fit=no'>
  <meta name='author' content='Bartosz Ciach'>
  <meta name='description'
    content='Check how your habits, weather conditions and daily activities affect your life & health.'>
  <meta name='keywords' content=''>
  <link rel='icon' type='image/svg+xml' href='assets/icons/favicon.ico'>
  <title>Doctor Crohn</title>
</head>
<body>
<div id='app'></div>
<script type="text/javascript" src="bundle.js"></script></body>
</html>

and my webpack.config.js file code below

const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')

const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
  template: './client/index.html',
  filename: 'index.html',
  inject: 'body'
})

module.exports = {
  entry: './client/index.js',
  output: {
    path: path.join(__dirname, '/build'),
    filename: 'bundle.js'
  },
  devServer: {
    contentBase: './client',
    hot: true,
    port: 3000
  },
  module: {
    loaders: [
      {
        enforce: 'pre',
        test: /\.(js|jsx)$/,
        exclude: /(node_modules|bower_components)/,
        loader: 'standard-loader',
      },
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        loaders: ['react-hot-loader/webpack', 'babel-loader']
      },
      {
        test: /\.scss$/,
        loaders: ['style-loader', 'css-loader', 'sass-loader']
      },
      {
        test: /\.(png|jpg)$/,
        loaders: ['url-loader', 'img-loader']
      },
      {
        test: /\.svg$/,
        loader: 'svg-url-loader'
      }
    ]
  },
  plugins: [HtmlWebpackPluginConfig]
}

I tried various things and unfortunately its still doesnt work as it should

Thomas Hennes

Just to bring closure to this question.

The last hurdle to get the app to render properly was to modify a couple of lines in the server.js file to point correctly to the /build directory. Considering the folder structure, and the location of the /build directory relative to the server.js file location, the following lines were updated:

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

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Serving a static html file from Express 4

From Dev

Send data to a route using Express JS when you are serving a static file for React-Router

From Dev

Using index.html for Hugo file serving

From Dev

How to modify a specific component of a list of component rendered using map in react?

From Dev

express server for serving Vue.js html file

From Dev

JavaScript not working on HTML file (Express)

From Dev

React component not rendered in loop

From Dev

React component is not being rendered

From Dev

Express: serving static files not working

From Dev

Serving Static Files in Express not working

From Dev

why component is not rendered in parent layout in react JS using react routing?

From Dev

Serving a static HTML page containing an image using Node JS / Express

From Dev

Serving HTML to build a simple SPA using Node.js/Express

From Dev

basic node.js html file serving not working

From Dev

Passing HTML element into React Component before Element is Rendered

From Dev

How to execute the js file once the react component is rendered?

From Dev

Mocking simple internal function using Jest for use in rendered React component

From Dev

Using Mocha to test props on a rendered SVG component in React

From Dev

How to update the header while the component is still rendered using React Navigation?

From Dev

Issues on animating a conditionally rendered component using React-spring

From Dev

component not rendered using react-router v5

From Dev

How to test a component rendered using React.createportal?

From Java

Express File Serving and Static Files

From Dev

Spring not serving html file

From Dev

Exclude index.html from express.static when serving React app with Express

From Dev

Serving Angular app using Express

From Dev

serving a project locally using express

From Dev

Scraping JS rendered page using Requests_HTML is not working as expected

From Dev

React component is getting rendered twice

Related Related

  1. 1

    Serving a static html file from Express 4

  2. 2

    Send data to a route using Express JS when you are serving a static file for React-Router

  3. 3

    Using index.html for Hugo file serving

  4. 4

    How to modify a specific component of a list of component rendered using map in react?

  5. 5

    express server for serving Vue.js html file

  6. 6

    JavaScript not working on HTML file (Express)

  7. 7

    React component not rendered in loop

  8. 8

    React component is not being rendered

  9. 9

    Express: serving static files not working

  10. 10

    Serving Static Files in Express not working

  11. 11

    why component is not rendered in parent layout in react JS using react routing?

  12. 12

    Serving a static HTML page containing an image using Node JS / Express

  13. 13

    Serving HTML to build a simple SPA using Node.js/Express

  14. 14

    basic node.js html file serving not working

  15. 15

    Passing HTML element into React Component before Element is Rendered

  16. 16

    How to execute the js file once the react component is rendered?

  17. 17

    Mocking simple internal function using Jest for use in rendered React component

  18. 18

    Using Mocha to test props on a rendered SVG component in React

  19. 19

    How to update the header while the component is still rendered using React Navigation?

  20. 20

    Issues on animating a conditionally rendered component using React-spring

  21. 21

    component not rendered using react-router v5

  22. 22

    How to test a component rendered using React.createportal?

  23. 23

    Express File Serving and Static Files

  24. 24

    Spring not serving html file

  25. 25

    Exclude index.html from express.static when serving React app with Express

  26. 26

    Serving Angular app using Express

  27. 27

    serving a project locally using express

  28. 28

    Scraping JS rendered page using Requests_HTML is not working as expected

  29. 29

    React component is getting rendered twice

HotTag

Archive