简体   繁体   English

如何强制Angular2返回JS而不是HTML?

[英]How do I force Angular2 to return JS instead of HTML?

I have an express app calling an Angular2 app. 我有一个名为Angular2应用程序的快速应用程序。 I am consistently getting Error: (SystemJS) Unexpected token < and searching for a bit has led me to find that this is typically due to the wrong content-type being returned. 我一直收到错误消息:(SystemJS)意外的令牌<并进行了一些搜索,使我发现这通常是由于返回了错误的内容类型。

I found that app.module.js is returning html and not .js, is this what is throwing the code off? 我发现app.module.js返回的是html而不是.js,这是在扔掉代码吗?

Error: (SystemJS) Unexpected token <

The error in detail is: 详细的错误是:

dashboard:21 Error: (SystemJS) Unexpected token <
    SyntaxError: Unexpected token <
        at eval (<anonymous>)
        at Object.eval (http://localhost:3000/main.js:3:20)
        at eval (http://localhost:3000/main.js:8:4)
        at eval (http://localhost:3000/main.js:9:3)
    Evaluating http://localhost:3000/app/app.module.js
    Evaluating http://localhost:3000/main.js
    Error loading http://localhost:3000/main.js
        at eval (<anonymous>)
        at Object.eval (http://localhost:3000/main.js:3:20)
        at eval (http://localhost:3000/main.js:8:4)
        at eval (http://localhost:3000/main.js:9:3)
    Evaluating http://localhost:3000/app/app.module.js
    Evaluating http://localhost:3000/main.js
    Error loading http://localhost:3000/main.js

Here is a snapshot of it as dev tools for network: 这是作为网络开发工具的快照: 在此处输入图片说明

This is what my express looks like: 这是我的快递的样子:

app.use('/styles.css', express.static(path.join(__dirname, '../angular/src/styles.css')));
app.use('/systemjs.config.js', express.static(path.join(__dirname, '../angular/src/systemjs.config.js')));
app.use('/systemjs-angular-loader.js', express.static(path.join(__dirname, '../angular/src/systemjs-angular-loader.js')));
app.use('/systemjs.config.extras.js', express.static(path.join(__dirname, '../angular/src/systemjs.config.extras.js')));
app.use('/main.js.map', express.static(path.join(__dirname, '../angular/src/main.js.map')));

app.use('/main.js', express.static(path.join(__dirname, '../angular/src/main.js')));
app.use(express.static(path.join(__dirname, '../angular')));
app.use('/api/', auditlog);
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, '../angular/src/index.html'));
});

Your express app is set up to serve index.html to any request, so you'll get that file back when you might usually get a 404. Given the error, I'm guessing it likely that you have a missing JS file, or possibly some missing SystemJS module mappings. 您的express应用程序已设置为可为任何请求提供index.html,因此通常会收到404时,您将获得该文件。鉴于该错误,我猜测您可能缺少JS文件,或者可能缺少一些SystemJS模块映射。 You can use the network inspector to find the missing file by looking in the preview tab of the request. 您可以使用网络检查器通过在请求的预览选项卡中查找来找到丢失的文件。

Consuming all routes is handy for Angular because of its routing, but it might be helpful to check if the client is requesting text/html before returning the app, returning 404 if not - This will make it easier to identify missing JS files on the client side. 由于Angular具有路由功能,因此使用所有路由都很方便,但是在返回应用程序之前检查客户端是否正在请求text / html可能会有所帮助,否则返回404-这将使在客户端上识别丢失的JS文件更加容易侧。

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

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