[英]Node JS and 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>
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:意外的令牌<
我的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打開節點靜態 )(或用Lighttpd或Apache HTTPD替換Node)來完成。
無論瀏覽器請求什么,您的服務器將始終返回相同的文件: index.html
。
您看到的錯誤是因為您的HTML文件引用了bundle.js
,當被請求時,會返回index.html
的內容。
您應該使用Web框架,這樣您就不必擔心這些事情。 如快遞 。
app.use(express.static('public'))
//server.js
const express = require('express')
const app = express()
// serve static assets from the public folder in project root
app.use(express.static('public'))
//
app.listen(8080, () => console.log('listening...'))
DOCS - https://expressjs.com/en/starter/static-files.html
祝好運。
您需要提供各種靜態文件,例如https://github.com/expressjs/serve-static#serve-files-with-vanilla-nodejs-http-server
var finalhandler = require('finalhandler')
var http = require('http')
var serveStatic = require('serve-static')
// Serve up public/ftp folder
var serve = serveStatic(__dirname)
// Create server
var server = http.createServer(function(req, res){
var done = finalhandler(req, res)
serve(req, res, done)
})
// Listen
server.listen(process.ENV.port || 3000)
只需在webpack.config.js文件中添加output: {publicPath: '/',}
即可 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.