Grunt dev服务器允许推送状态

时间:2016-09-02 14:23:26

标签: javascript gruntjs

我正在尝试设置我的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']);

};

2 个答案:

答案 0 :(得分:0)

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

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

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

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

答案 1 :(得分:0)

请勿使用grunt为SPA部署本地dev pushstate服务器。

在项目目录中,安装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