Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Uncaught SyntaxError: Unexpected token <' in Node.js

I am getting the below screen-shot error when I try to serve my client-side code. When I am trying to run node server/server.js:

Enter image description here

The below is my server.js code...

app.use(express.static(path.join(__dirname, "public")));
app.use(logger('dev'));
app.use(bodyParser.json({limit: '50mb'}));

app.all('/*', function(req, res, next){
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
    res.header("Access-Control-Allow-Headers", "Content-type,Accept,X-Access-Token,X-Key");

    if(req.method === 'OPTIONS'){
        res.status(200).end();
    } else {
        next();
    }

});

app.all("/api/v1/*", [require('./middlewares/validateRequest')]);
app.use("/", require("./routes"));

app.use(function(req, res, next){
    var err = new Error("Not found");
    err.status = 404;
    next(err);
});

Inside my routes/index.js, I have the following for get request.

router.get('*', function(req, res) {
    res.sendfile('./public/index.html');
});
like image 252
ShankarGuru Avatar asked Dec 05 '15 13:12

ShankarGuru


People also ask

What is “uncaught SyntaxError unexpected token ‘export’”?

The error “Uncaught SyntaxError Unexpected token ‘export'” happens for two reasons: In a Node.js application, using the ES6 Module syntax without specifying a type to module in package.json.

What are syntax errors in JavaScript?

SyntaxError: Unexpected token The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.

What is uncaught SyntaxError?

Uncaught SyntaxError: Unexpected token Aug 24 2016Ibrahim Diallo This is a common error in JavaScript, and it is hard to understand at first why it happens. But if you bear with me and remember that Bugs are a good thingyou will be on your way in no time. TL;DR The JavaScript file you are linking to is returning 404 page.


2 Answers

Usually when the browser requests a JavaScript file, the server sends an HTML file. This is due to rules like app.get('*'.... So we need to tell the server to send the static files first and then declare the rules, like shown below:

// Declare static folder to be served. It contains the JavaScript code, images, CSS, etc.
app.use(express.static('build'));

// Serve the index.html for all the other requests so that the
// router in the JavaScript application can render the necessary components
app.get('*', function(req, res){
  res.sendFile(path.join(__dirname + '/build/index.html'));
  //__dirname : It will resolve to your project folder.
});
like image 178
Jonathan Morales Vélez Avatar answered Sep 22 '22 07:09

Jonathan Morales Vélez


I found the solution. The Node.js application tries to compile the JavaScript files inside the public folder, that makes a mistake; you defined the public folder like this:

app.use(express.static(path.join(__dirname, 'public')));

Try to define it using a virtual directory by adding a specific virtual folder name like this:

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

This should be work;

like image 21
Mostaffa Ossman Avatar answered Sep 21 '22 07:09

Mostaffa Ossman