节点JS和Webpack意外的标记<
我开始研究节点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和访问地址(本地主机:3000)我在控制台中得到错误。
bundle.js:1未捕获的SyntaxError:意外的标记<
另外我的JS文件不运行。 有人可以提出一些解决办法吗?
提前致谢
您的服务器:
var Server = http.createServer(function(request, response) { var filename = path.join(__dirname, 'index.html');
…被configuration为忽略请求中的所有内容并始终返回index.html
的内容。
所以当浏览器请求/assets/bundle.js
,会给出index.html
(以及错误,因为那不是JavaScript)。
您需要注意path并使用适当的内容types提供适当的内容。
这可能是最好的find一个静态文件服务模块(谷歌变成节点静态 )的节点(或replace像Lighttpd或Apache HTTPD的节点)。
如果您想要提供dynamic内容以及静态内容,则Express是一个stream行的select(并且支持静态文件 )。
无论浏览器要求什么,您的服务器将始终返回相同的确切文件: index.html
。
你看到的错误是因为你的HTML文件有一个bundle.js
的引用,当被请求的时候,它返回index.html
的内容。
你应该使用一个Web框架,这样你就不必担心这些事情。 例如Express 。
Express用户:
让节点服务器知道你的静态文件的位置
注意这一行>> 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)