javascript - Node JS 和 Webpack 意外 token <

标签 javascript node.js webpack

我已经开始学习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>

app.js

var http = require("http"),
    path = require('path')
    fs = require("fs"),
    colors = require('colors'),
    port = 3000;

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

  fs.readFile(filename, function(err, file) {
    if(err) {        
      response.writeHead(500, {"Content-Type": "text/plain"});
      response.write(err + "\n");
      response.end();
      return;
    }

    response.writeHead(200);
    response.write(file);
    response.end();
  });
});

Server.listen(port, function() {
  console.log(('Server is running on http://localhost:'+ port + '...').cyan);

webpack.config.js

module.exports = {
    entry: './src/index.js',
    output: {
        path: __dirname + '/assets',
        filename: 'bundle.js'
    }
}

更新 bundle.js

/******/ (function(modules) { // webpackBootstrap
/******/    // The module cache
/******/    var installedModules = {};

/******/    // The require function
/******/    function __webpack_require__(moduleId) {

/******/        // Check if module is in cache
/******/        if(installedModules[moduleId])
/******/            return installedModules[moduleId].exports;

/******/        // Create a new module (and put it into the cache)
/******/        var module = installedModules[moduleId] = {
/******/            exports: {},
/******/            id: moduleId,
/******/            loaded: false
/******/        };

/******/        // Execute the module function
/******/        modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

/******/        // Flag the module as loaded
/******/        module.loaded = true;

/******/        // Return the exports of the module
/******/        return module.exports;
/******/    }


/******/    // expose the modules object (__webpack_modules__)
/******/    __webpack_require__.m = modules;

/******/    // expose the module cache
/******/    __webpack_require__.c = installedModules;

/******/    // __webpack_public_path__
/******/    __webpack_require__.p = "";

/******/    // Load entry module and return exports
/******/    return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports) {

    alert('Hello');

/***/ }
/******/ ]);

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

bundle.js:1 Uncaught SyntaxError: Unexpected token <

我的 JS 文件也没有运行。 有人可以提出一些解决方法吗?

提前致谢

最佳答案

您的服务器:

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

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

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

您需要注意路径并使用适当的内容类型提供适当的内容。

这可能最好通过为 Node 找到一个静态文件服务模块(Google 出现 node-static)(或将 Node 替换为 Lighttpd 或 Apache HTTPD 之类的东西)。

如果您想提供动态内容和静态内容,那么 Express是一个流行的选择(并且有 support for static files )。

关于javascript - Node JS 和 Webpack 意外 token <,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33260093/

相关文章:

css - 如何在 2 中编译 2 个 less 文件 !!! css文件在特定的地方?

reactjs - 仅包含打包包 ReactJS 中使用过的导入

javascript - css-loader URL root 不修改 url

Javascript - 将参数传递给非匿名函数

带有 2 个数组的 javascript for 循环

node.js - react native 窗口,产生 npm ENOENT 错误

javascript - 在 Node-soap (node.js) 中发送带有数组的请求

javascript - 从 javascript 调用操作方法时 Asp、Net MVC 应用程序的安全问题

javascript - expectHEAD 已记录但未实现?

node.js - Nodejs HTTPS客户端超时不关闭TCP连接