简体   繁体   English

导入时出现“ Uncaught SyntaxError:意外的令牌<”错误

[英]“Uncaught SyntaxError: Unexpected token <” error while importing

I'm trying to import .js file but facing Uncaught SyntaxError: Unexpected token < error while importing. 我正在尝试导入.js文件,但遇到Uncaught SyntaxError:导入时出现意外的令牌<错误。 I just dived into MEAN stack and facing these issues while importing files. 我刚刚进入MEAN堆栈,并在导入文件时遇到了这些问题。 Even the angular files are not getting imported . 即使角度文件也不会导入。

server.js server.js

//Variables------------------------------
var express = require('express');
var app = express();
var port = process.env.PORT || 8080;
var morgan = require('morgan');
var mongoose = require('mongoose');
var User = require('./app/models/user');
var bodyParser = require('body-parser');
var router = express.Router();
var appRouters = require('./app/routes/api')(router);
var path = require('path');
//---------------------------------------

//Middleware--------------------------
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({ extended: true })); // for parsing 
application/x-www-form-
app.use(bodyParser.json()); // for parsing application/json
app.use('/static', express.static(__dirname+'/public'))
app.use('/api',appRouters);


 mongoose.connect('mongodb://localhost:27017/test',function(err){
  if(err){
  console.log('Not connected '+err);
   }else{
  console.log('Connected to db');
   }
   });

//-------Fontend Routes
  app.get('*',function(req,res){
  res.sendFile(path.join(__dirname + '/public/app/views/index.html'));
 });


 app.listen(port,function(){
 console.log('Running the server');
 });

index.html index.html

<html>
 <head>
   import file
   **<script src="app/app.js"></script>**
   </head>

   <body>
   <div class="container">
   <nav class="navbar navbar-dark bg-dark">
   <span class="navbar-brand mb-0 h1">Navbar</span>
   </nav>
   </div>

   </body>
  </html>

app.js app.js

 console.log("testing");
 app.get('*',function(req,res){ res.sendFile(path.join(__dirname + '/public/app/views/index.html')); }); 

You said that for * , ie any URL other than those starting with /api or /static (which you defined first), to serve up the content of index.html . 您说过,对于* ,即/api/static开头(您首先定义)的那些URL以外的任何URL,都可以提供index.html的内容。

You then have: 然后,您将拥有:

<script src="app/app.js"></script>

… which is going to get index.html and try to treat it as JavaScript. …这将获取index.html并尝试将其视为JavaScript。 Which it isn't. 不是。

Get the URLs to your scripts correct (or get the server to serve the right data for the URLs you are requesting). 正确获取脚本的URL(或获取服务器以为您请求的URL提供正确的数据)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM