简体   繁体   English

使用 vanilla spa 时出现错误 router.js:66 GET http://localhost:3000/views/Category.html 404 (Not Found)

[英]when use vanilla spa i have an error router.js:66 GET http://localhost:3000/views/Category.html 404 (Not Found)

I have a project on vanilla JS, I use Webpack.我有一个关于 vanilla JS 的项目,我使用 Webpack。 And i change html container contents with spa.我用 spa 更改 html 容器内容。 I write spa by this article https://medium.com/better-programming/js-vanilla-script-spa-1b29b43ea475 .我通过这篇文章写 spa https://medium.com/better-programming/js-vanilla-script-spa-1b29b43ea475 But I have an error router.js:66 GET http://localhost:3000/views/Category.html 404 (Not Found).但是我有一个错误 router.js:66 GET http://localhost:3000/views/Category.html 404 (Not Found)。 what's wrong in my code?我的代码有什么问题? my header remains static, only the contents of the main container change.我的标题保持静态,只有主容器的内容发生变化。 Structure of my project:我的项目结构:

-src
 -routing
  -route.js
  -router.js
 -views
  -Category.html
  -Page2.html
 -app.js
 -index.html
 -style.css

The code: route.js:代码:route.js:

'use stict';

let Route = function (name, htmlName, defaultRoute) {
    try {
        if(!name || !htmlName) {
            throw 'error: name and htmlName params are mandatories';
        }
        this.constructor(name, htmlName, defaultRoute);
    } catch (e) {
        console.error(e);
    }
}

Route.prototype = {
    name: undefined,
    htmlName: undefined,
    default: undefined,
    constructor: function (name, htmlName, defaultRoute) {
        this.name = name;
        this.htmlName = htmlName;
        this.default = defaultRoute;
    },
    isActiveRoute: function (hashedPath) {
        return hashedPath.replace('#', '') === this.name; 
    }
}

export default Route;

router.js:路由器.js:

'use strict';

let Router = function (routes) {
    try {
        if (!routes) {
            throw 'error: routes param is mandatory';
        }
        this.constructor(routes);
        this.init();
    } catch (e) {
        console.error(e);   
    }
}

Router.prototype = {
    routes: undefined,
    rootElem: undefined,
    constructor: function (routes) {
        this.routes = routes;
        this.rootElem = document.getElementById('main');
    },
    init: function () {
        var r = this.routes;
        (function(scope, r) { 
            window.addEventListener('hashchange', function (e) {
                scope.hasChanged(scope, r);
            });
        })(this, r);
        this.hasChanged(this, r);
    },
    hasChanged: function(scope, r){
        if (window.location.hash.length > 0) {
            for (var i = 0, length = r.length; i < length; i++) {
                var route = r[i];
                if(route.isActiveRoute(window.location.hash.substr(1))) {
                    scope.goToRoute(route.htmlName);
                }
            }
        } else {
            for (var i = 0, length = r.length; i < length; i++) {
                var route = r[i];
                if(route.default) {
                    scope.goToRoute(route.htmlName);
                }
            }
        }
    },
    goToRoute: function (htmlName) {
        (function(scope) { 
            var url = 'views/' + htmlName,
                xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function () {
                if (this.readyState === 4 && this.status === 200) {
                    scope.rootElem.innerHTML = this.responseText;
                }
            };
            xhttp.open('GET', url, true);
            xhttp.send();
        })(this);
    }
};

export default Router;

app.js:应用程序.js:

import Route from './routing/route.js';
import Router from './routing/router.js';

(function () {
  function init() {
      var router = new Router([
          new Route('Category', 'Category.html', true),
          new Route('ActionSetA', 'ActionSetA.html'),
          new Route('ActionSetB', 'ActionSetB.html'),
          new Route('AnimalSetA', 'AnimalSetA.html'),
          new Route('AnimalSetB', 'AnimalSetB.html'),
          new Route('Clothes', 'Clothes.html'),
          new Route('Emotions', 'Emotions.html'),
      ]);
  }
  init();
}());

Here is my webpack:这是我的网络包:

const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebPackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
  entry: './src/app.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'main.js'
  },
  mode: 'development',
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.html$/,
        use: [
          {
            loader: 'html-loader',
            options: { minimize: false },
          },
        ],
      },
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader'],
      },
      {
        test: /\.scss$/,
        use: [
          {
            loader: MiniCssExtractPlugin.loader,
          },
          {
            loader: 'css-loader',
          },
          {
            loader: 'sass-loader',
          },
        ],
      },
      {
        test: /\.(png|jpe?g|gif|svg)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              outputPath: 'images',
              name: '[name].[ext]',
            },
          },
          {
            loader: 'image-webpack-loader',
            options: {
              bypassOnDebug : true,
              mozjpeg: {
                progressive: true,
                quality: 75
              },
              optipng: {
                enabled: false,
              },
              pngquant: {
                quality: [0.65, 0.90],
                speed: 4
              },
              gifsicle: {
                interlaced: false,
                optimizationLevel: 1
              },
              webp: {
                quality: 75
              }
            }
          }
        ],
      },
      {
        test: /\.(woff|woff2|ttf|otf|eot)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              outputPath: 'fonts',
            },
          },
        ],
      },
    ],
  },
  plugins: [
    new  HtmlWebPackPlugin({
      template: './src/index.html',
      filename: './index.html'
    }),
    new MiniCssExtractPlugin({
      filename: '[name].css',
      chunkFilename: '[id].css',
    }),
    new CopyWebpackPlugin([
      {from: './src/images', to: './images'},
    ]),
  ],
  devServer: {
    contentBase: path.join(__dirname, 'dist'),
    compress: true,
    port: 3000,
  },
};

Your path seems wrong http://localhost:3000/views/Category.html relative to to your server.相对于您的服务器,您的路径似乎是错误的 http://localhost:3000/views/Category.html。

Could be some misconfiguration wrt the root path your server is running.可能是您的服务器运行的根路径配置错误。

Apart from that it's quite difficult to guess the exact error.除此之外,很难猜测确切的错误。 Take a look at relative path from server, check your server/webpack configuration.查看来自服务器的相对路径,检查您的服务器/webpack 配置。

Update: Post your webpack update, can you check whether your "dist" folder has the correct folder/paths for views as I suppose your webpack is bundling and deploying your file to "dist" folder.更新:发布您的 webpack 更新,您能否检查您的“dist”文件夹是否具有正确的视图文件夹/路径,因为我认为您的 webpack 正在捆绑并将您的文件部署到“dist”文件夹。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React.js / Firebase 应用程序中的 Axios 错误(404):POST http://localhost:3000/login 404(未找到) - Axios error (404) in React.js / Firebase app: POST http://localhost:3000/login 404 (Not Found) GET http://localhost:3000/search 404 (Not Found)REACT.JS, Node.js 和 Axios - GET http://localhost:3000/search 404 (Not Found)REACT.JS, Node.js and Axios GET http://localhost:3000/dashboard/js/appclient.js net::ERR_ABORTED 404(未找到) - GET http://localhost:3000/dashboard/js/appclient.js net::ERR_ABORTED 404 (Not Found) "GET http:\/\/localhost:3000\/public\/js\/test.js net::ERR_ABORTED 404(未找到)" - GET http://localhost:3000/public/js/test.js net::ERR_ABORTED 404 (Not Found) 识别:9 GET http://localhost:3000/script.js net::ERR_ABORTED 404(未找到) - recognition:9 GET http://localhost:3000/script.js net::ERR_ABORTED 404 (Not Found) GET http://localhost:3000/insert.js net::ERR_ABORTED 404(未找到) - GET http://localhost:3000/insert.js net::ERR_ABORTED 404 (Not Found) GET http:// localhost:3000 / projects / assets / jquery.masonry.js 404(未找到) - GET http://localhost:3000/projects/assets/jquery.masonry.js 404 (Not Found) GET http://localhost:3000/bundle.js net::ERR_ABORTED 404(未找到) - GET http://localhost:3000/bundle.js net::ERR_ABORTED 404 (Not Found) GET http://127.0.0.1:3000/build/three.module.js net::ERR_ABORTED 404 (Not Found) 尝试将轨道控制与 three.js 一起使用时出错 - GET http://127.0.0.1:3000/build/three.module.js net::ERR_ABORTED 404 (Not Found) Error when trying to use orbit controls with three.js POST http:// localhost:3000/404(未找到) - POST http://localhost:3000/ 404 (Not Found)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM