感觉这个问题应该不是太难,只是我觉得他貌似没什么问题,求大神解惑
我用webpack
构建项目打包后文件如下
test
|___showlive.html
|___resources
|___js
|__showlive.js
然后在上面test
目录下加了一个express
启动项node-server
|___node-server
|___package.json
|___app.js
这是app.js
里的内容
var path = require('path');
var express = require('express');
var bodyParser = require('body-parser');
var compression = require('compression');
var ejs = require('ejs');
var port = process.env.PORT || 1983;
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.engine('html', ejs.__express);
app.set('view engine', 'html');
var nodeDirname = __dirname.split(/(\/|\\)node-server/)[0];
app.set('views', nodeDirname);
app.use(compression()); //开启 gzip
app.get('/*', function(req, res) {
res.render('showlive');
});
app.use('/resources', express.static(path.join(nodeDirname, 'resources')));
app.listen(port, function(){
console.log("node启动 监听端口:" + port);
});
这是showlive.html
<!DOCTYPE html>
<html lang="zh-cn">
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>webpack</title>
</head>
<body>
<div id="app"></div>
<script type="text/javascript" src="resources/js/showlive.js"></script>
</body>
</html>
这是showlive.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) {
'use strict';
console.log('1111111111111111111111111');
/***/ }
/******/ ]);
但是启动运行node localhost:1983
下面这里路径出错,但看上去是正常啊,这是怎么回事?
这难道不是把所有请求都放在这里处理了吗.
所以当服务器请求showlive.js的时候也放到这里.结果就是返回了showlive.html.
结果就报了
unexpected token <