我正在尝试导入.js文件,但在导入时遇到Uncaught SyntaxError:意外的令牌<错误。我刚刚进入MEAN堆栈,并在导入文件时遇到了这些问题。甚至没有角度文件也不会导入。
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
<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
console.log("testing");
答案 0 :(得分:2)
app.get('*',function(req,res){ res.sendFile(path.join(__dirname + '/public/app/views/index.html')); });
您说过,对于*
,即除以/api
或/static
开头的那些URL(您首先定义)之外的任何URL,都要提供index.html
的内容
您将拥有:
<script src="app/app.js"></script>
…这将得到index.html
并尝试将其视为JavaScript。不是。
正确获取脚本的URL(或让服务器为您请求的URL提供正确的数据)。