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} `);
});
2
  • 1
    Your app is sending back index.html when the browser is requesting your javascript files. Take a look at stackoverflow.com/questions/26349497/… Commented Aug 24, 2018 at 3: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. Commented Aug 24, 2018 at 5:12

1 Answer 1

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} `);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.