Node JS 和 Webpack Unexpected token < [英] Node JS and Webpack Unexpected token <

查看:20
本文介绍了Node JS 和 Webpack Unexpected token <的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始研究 Node JS.

这是我的文件.

index.html

<!DOCTYPE html>
<html>
<head>
</head>
<body>
  <div id="app">
    <h1>Hello<h1>
  </div>
  <script src='assets/bundle.js'></script>
</body>
</html>

<script src='assets/bundle.js'></script>

app.js

app.js

webpack.config.js

webpack.config.js

UPDATE
bundle.js

更新bundle.js

So, when i  hit a app.js and visit the address (localhost:3000) i get the error in console.

因此,当我点击 app.js 并访问地址 (localhost:3000) 时,我在控制台中收到错误消息.

bundle.js:1 Uncaught SyntaxError: Unexpected token <

bundle.js:1 Uncaught SyntaxError: Unexpected token <

Also my JS file doesnt run. Could someone suggest something to fix it?

我的 JS 文件也没有运行.有人可以建议修复它吗?

Thanks in advance

提前致谢

推荐答案

您的服务器:

var Server = http.createServer(function(request, response) {
  var filename = path.join(__dirname, 'index.html');

... 配置为忽略请求中的所有内容并始终返回 index.html 的内容.

… is configured to ignore everything in the request and always return the contents of index.html.

因此,当浏览器请求 /assets/bundle.js 时,它会得到 index.html(并且会出现错误,因为那不是 JavaScript).

So when the browser requests /assets/bundle.js it is given index.html (and errors because that isn't JavaScript).

您需要注意路径并以适当的内容类型提供适当的内容.

You need to pay attention to the path and serve up appropriate content, with the appropriate content type.

这可能最好通过找到一个静态文件服务模块来完成(Google 发现了 node-static) 用于 Node(或用 Lighttpd 或 Apache HTTPD 之类的东西替换 Node).

This would probably be best done by finding a static file serving module (Google turns up node-static) for Node (or replacing Node with something like Lighttpd or Apache HTTPD).

如果您想同时提供动态内容和静态内容,Express 是一种流行的选择(并且有支持静态文件).

If you want to serve up dynamic content as well as static content, then Express is a popular choice (and has support for static files).

这篇关于Node JS 和 Webpack Unexpected token &lt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆