1

I built an app on angular 6 and directly able to open index.html from dist folder but when i tried it opening from my node server, error is coming in console as:

Uncaught SyntaxError: Unexpected token <
polyfills.js:1 Uncaught SyntaxError: Unexpected token <
styles.js:1 Uncaught SyntaxError: Unexpected token <
vendor.js:1 Uncaught SyntaxError: Unexpected token <
main.js:1 Uncaught SyntaxError: Unexpected token <

with line 1 as < ! doctype html >

server.js

const express = require('express');
const path = require('path');
const app = express();
app.use(express.static(path.join(__dirname,'dist')));
app.get('*',(req,res)=>{
    res.sendFile(path.join(__dirname,'dist/ang-node/index.html'));
});

const port = process.env.PORT || 4000;

app.listen(port,(req,res)=>{
    console.log(`server started on port ${port} `);
});
anshul goel
  • 78
  • 1
  • 10
  • 1
    Your app is sending back `index.html` when the browser is requesting your javascript files. Take a look at https://stackoverflow.com/questions/26349497/node-express-with-static-html-how-to-route-all-requests-to-index-html – user184994 Aug 24 '18 at 03:23
  • index.html will come on the sendFile request, in the middleware i am passing the static path of the parent directory. The file is getting up in the sources but in the console some errors are coming. – anshul goel Aug 24 '18 at 05:12

1 Answers1

0

As your error you may not configured the right public directory for the angular, Try this,

const express = require('express');
const path = require('path');
const app = express();
app.use(express.static(path.join(__dirname,'dist/ang-node/public'))); // <== correct public directory here
app.get('*',(req,res)=>{
    res.sendFile(path.join(__dirname,'dist/ang-node/index.html')); // <== Make sure this is dist/ang-node not dist
});

const port = process.env.PORT || 4000;

app.listen(port,(req,res)=>{
    console.log(`server started on port ${port} `);
});
Janith
  • 2,730
  • 16
  • 26