1

Hi I am a newbie and started to learn about node recently. I took an Heroku tutorial on websockets (https://devcenter.heroku.com/articles/node-websockets) and adapted it for a specific project I was working on. In the example code there was a single index.html file with some embedded javascript. I moved this script out to a separate file and referenced it in the HTML. Everything worked fine locally but doesn't work when i deploy to Heroko. I chatted with the very helpful team at Heroku who informed me that my server side code is serving up all files as HTML and I need to change the code. They gave me some pointers and I tried as many things as I could over several days but to no avail. In the end they recommended coming to this forum as a way to solve the problem as it is beyond their scope. The existing code that serves up the index.html file is as follows:

const express = require('express');
const SocketServer = require('ws').Server;
const path = require('path');
const PORT = process.env.PORT || 3000;
const INDEX = path.join(__dirname, 'index.html');
const server = express()
.use((req, res) => res.sendFile(INDEX) )
.listen(PORT, () => console.log(Listening on ${ PORT }));

At first i edited this to include the line:

app.use(express.static('public'))

but this didn't work. I then amended as follows and it still doesn't work:

const INDEX = path.join(__dirname, 'index.html');
const JS = path.join(__dirname, 'client.js');

const server = express()
.use((req, res) => {
res.sendFile(INDEX);
res.sendFile(JS);

I have looked at other tutorials that work when i run them in isolation but when I try to adapt my above code it simply doesn't work. I would really appreciate if someone out there could point me in the right direction.

BTW this is what Heroku told me:

"To explain a bit further this error Uncaught SyntaxError: Unexpected token < is because the URL for http://thawing-journey-33085.herokuapp.com/client.js isn't serving a javascript file but is instead trying to serve the HTML for the homepage. This suggests you have an issue with the routing in your application which you'll need to review. This is probably because your server.js file doesn't check for any particular URL before sending the index.html file."

Thanks

LankyDonkey
  • 49
  • 2
  • 6

1 Answers1

1

I serve my static files like this:

// define the folder that will be used for static assets
app.use(Express.static(path.join(__dirname, '../public')));

// handle every other route with index.html, which will contain
// a script tag to your application's JavaScript file(s).
app.get('*', function (request, response){
    response.sendFile(path.resolve(__dirname, '../public', 'index.html'));
});

This way i set the static folder in the express.static middleware so i can serve the files. And then i redirect all url request to the index.html

To know more: express static

sendra
  • 658
  • 1
  • 9
  • 21
  • Hi Thanks for the response. I finally managed to get it working using your suggestion but first I had to rewrite my code to remove the javascript 6 syntax that was causing me problems. If anyone is interested the working code is as follows: app.use('/',express.static(__dirname)); app.get('/',function(req,res){ res.sendFile(INDEX); }) var server= app.listen(PORT,function(){ console.log("Listening on port: "+ PORT); }) – LankyDonkey Dec 12 '16 at 14:04