owais
owais

Reputation: 4922

Expressjs static javascript file not loading

I'm setting up express server with create-react-app.

AT console I'm getting

Uncaught SyntaxError: Unexpected token < bundle.js:1

When I click on error it shows me homepage html. While using morgan logger its giving me 200 ok.

GET /static/js/bundle.js 304 1.145 ms - -

Also when i view source click on css link it shows me css while click on javascript link it shows me blank page(homepage html).

Here is the the server code.

var express = require('express');
var bodyParser = require('body-parser');
var logger = require('morgan');
var path = require ('path');

var data = {};


express()
    .use(logger('dev'))
    .use(express.static(path.join(__dirname, 'flashcard-app/build')))

    .use(bodyParser.json())
    .get('/api/test', (req, res) => res.json("Great news every thing is working fine."))
    .get('/api/data', (req, res) => res.json(data))
    .post('/api/data', (req, res) => res.json(data = req.body))
    .get('*', function(req, res) {
        console.log('serving path:', path.join(__dirname+'/flashcard-app/build/index.html'));
         res.sendFile(path.join(__dirname, 'flashcard-app/build/index.html'));
    })
    .listen(3333, function(){
        console.log('server running at 3333');
    });

Also this is custom server.js file not one that provided by express-generator. I'm running by node server_old.js instead of using ./bin/www

Help comment tips appreciated.

folder-structure

Upvotes: 0

Views: 497

Answers (1)

Mukesh Sharma
Mukesh Sharma

Reputation: 9032

You are trying to fetch /static/js/bundle.js, but there is no bundle.js in static/js.

I think you are trying to use index.html that tries to bundle.js while your webpack config builds main.[hash].js

Upvotes: 1

Related Questions