【问题标题】:Grunt dev server to allow push statesGrunt 开发服务器允许推送状态
【发布时间】:2017-01-10 16:58:56
【问题描述】:

我正在尝试设置我的 grunt 服务器以允许推送状态。

在无数次谷歌搜索和阅读 SO 帖子之后,我无法弄清楚如何做到这一点。

我不断收到如下错误。

有人知道如何解决这个问题吗?

未找到“连接”目标。警告:任务“连接”失败。使用 --force 继续。

在我看来,我在下面定义了targets

open: {
    target: 'http://localhost:8000'
 }

完整代码如下:

var pushState = require('grunt-connect-pushstate/lib/utils').pushState;

module.exports = function(grunt) {

  // Project configuration.
    grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),

        connect: {
        options: {
          hostname: 'localhost',
          port: 8000,
          keepalive: true,
          open: {
             target: 'http://localhost:8000'
          },
          middleware: function (connect, options) {
            return [
              // Rewrite requests to root so they may be handled by router 
              pushState(),

              // Serve static files 
              connect.static(options.base)
            ];
          } 
        }
      }
    });

  grunt.loadNpmTasks('grunt-contrib-uglify'); // Load the plugin that provides the "uglify" task.
  grunt.loadNpmTasks('grunt-contrib-connect'); // Load the plugin that provides the "connect" task.

  // Default task(s).
  grunt.registerTask('default', [ 'connect']);

};

【问题讨论】:

  • 这样的情况是我切换到gulp的原因。此外,该软件包已被贬低。
  • @MichaelCole 遇到了与gulp 相同的问题!关于如何使用gulp 的任何想法?看来连谷歌都不知道!
  • 对不起,我帮不上忙。这是相当古老的技术——我已经构建了 MEAN 应用程序并且不需要它。你确定你需要它吗?
  • 我想要一种在本地服务器上允许推送状态的方法。你知道解决这个问题的其他方法吗?我不是专家

标签: javascript gruntjs


【解决方案1】:

大多数 SPA 框架中已包含推送状态,因此除非您正在构建框架,否则您可能不需要它。

角度:https://scotch.io/tutorials/pretty-urls-in-angularjs-removing-the-hashtag

反应:How to remove the hash from the url in react-router

这看起来像一个 grunt 构建脚本来编译应用程序以提供服务。所以我不确定你会如何在构建过程中使用 pushStates。您可能正在尝试解决错误的问题。

【讨论】:

    【解决方案2】:

    不必为您的 SPA 部署本地开发推送状态服务器而烦恼。

    在你的项目目录下,安装https://www.npmjs.com/package/pushstate-server

    npm i pushstate-server -D

    然后要启动它,请在项目的 package.json 中添加 script 条目:

    … "scripts": { "dev": "pushstate-server" } …

    这样你现在可以开始运行npm run dev

    所有通常以 404 结尾的请求现在将重定向到 index.html

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      • 2016-09-04
      • 2012-04-22
      • 1970-01-01
      相关资源
      最近更新 更多