小编Dan*_*ani的帖子

跨源请求被阻止

所以我有这个Go http处理程序,它将一些POST内容存储到数据存储区中,并检索一些其他信息作为响应.在后端我使用:

func handleMessageQueue(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Access-Control-Allow-Origin", "*")
    if r.Method == "POST" {

        c := appengine.NewContext(r)

        body, _ := ioutil.ReadAll(r.Body)

        auth := string(body[:])
        r.Body.Close()
        q := datastore.NewQuery("Message").Order("-Date")

        var msg []Message
        key, err := q.GetAll(c, &msg)

        if err != nil {
            c.Errorf("fetching msg: %v", err)
            return
        }

        w.Header().Set("Content-Type", "application/json")
        jsonMsg, err := json.Marshal(msg)
        msgstr := string(jsonMsg)
        fmt.Fprint(w, msgstr)
        return
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的firefox OS应用程序中,我使用:

var message = "content";

request = new XMLHttpRequest();
request.open('POST', 'http://localhost:8080/msgs', true);

request.onload = function () { …
Run Code Online (Sandbox Code Playgroud)

ajax google-app-engine go cors firefox-os

21
推荐指数
1
解决办法
16万
查看次数

Webpack 2 eslint-loader自动修复

在webpack 1.x中,我可以使用webpack配置中的eslint属性来启用自动修复我的linting错误:

...

module.exports = {
  devtool: 'source-map',
  entry: './src/app.js',
  eslint: {
    configFile: '.eslintrc',
    fix: true
  },

...
Run Code Online (Sandbox Code Playgroud)

但是,在webpack 2.x中,因此我无法使用自动修复功能,因为我不知道在我的webpack配置中将它设置在何处.在我的webpack中使用eslint属性configFile抛出一个WebpackOptionsValidationError.

eslint webpack webpack-2

18
推荐指数
1
解决办法
9638
查看次数

使用html/template在切片中执行格式化的时间

我正在制作这个简单的网络服务器,可以托管我的博客,但无论我做什么; 我无法在我的html /模板中执行正确的格式化时间.

这是我做的:

我创建了这个结构:

type Blogpost struct {
    Title   string
    Content string
    Date    time.Time
}
Run Code Online (Sandbox Code Playgroud)

接下来我创建了这个小函数,它从Appengine数据存储区中检索具有相应标题/日期的博客帖子并将其作为切片返回:

func GetBlogs(r *http.Request, max int) []Blogpost {
    c := appengine.NewContext(r)
    q := datastore.NewQuery("Blogpost").Order("-Date").Limit(max)
    bp := make([]Blogpost, 0, max)
    q.GetAll(c, &bp)
    return bp
}
Run Code Online (Sandbox Code Playgroud)

最后,在blogHandler中,我使用以下方法根据从Appengine数据存储中检索到的数据创建一个切片:

blogs := GetBlogs(r, 10)
Run Code Online (Sandbox Code Playgroud)

现在当我执行这样名为博客的模板时,博客的日期将被解析为默认日期:

blog.Execute(w, blogs) // gives dates like: 2013-09-03 16:06:48 +0000 UTC
Run Code Online (Sandbox Code Playgroud)

所以,我,作为我的Golang n00b,会说像下面这样的函数会给我我想要的结果

blogs[0].Date = blogs[0].Date.Format("02-01-2006 15:04:05") // Would return like 03-09-2013 16:06:48, at least when you print the formatted date that is.
Run Code Online (Sandbox Code Playgroud)

然而,这会导致类型冲突,我尝试使用以下方法解决:

blogs[0].Date, …
Run Code Online (Sandbox Code Playgroud)

google-app-engine go

12
推荐指数
2
解决办法
1万
查看次数

Golang模板在不同的目录级别上提供css

我正在使用golang的"html/template"包来使用相同的_base.html作为框架在多个页面上提供内容.我将多个html文件(_base.html和内容文件)合并为一个.

func main() {
http.HandleFunc("/", indexHandler)
http.HandleFunc("/blog/", blogHandler)
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("http/css"))))
http.ListenAndServe(":1337", nil)
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
index := template.Must(template.ParseFiles(
    "http/html/_base.html",
    "http/html/index.html",
))
index.Execute(w, nil)
}

func blogHandler(w http.ResponseWriter, r *http.Request) {
blog := template.Must(template.ParseFiles(
    "http/html/_base.html",
    "http/html/blog.html",
))
blog.Execute(w, nil)
}
Run Code Online (Sandbox Code Playgroud)

在我的网络服务器的根目录上这样做我的css渲染得很好,因为_base.html中我的.css的html链接标记使用以下命令指向正确的目录:

<link href="css/style.css" rel="stylesheet">
Run Code Online (Sandbox Code Playgroud)

然而,当我从/ to/blog /导航时,我的css降低了一级(或者我升级了,但是你想看到它)所以css href突然指向/blog/css/style.css并且因此它不会呈现.

这可以很容易地修复,说明我与_base.html合并的每个内容文件中的css级别,但是我觉得必须有另一种更清洁,不同的方式.在这种情况下,我的直觉是否存在误导?

html css go

9
推荐指数
1
解决办法
8454
查看次数

MapBox GL JS标记偏移量

我正在使用MapBox GL JS来创建带有自定义标记的地图:

var marker = new mapboxgl.Marker(container)
    .setLngLat([
        datacenters[country][city].coordinates.lng,
        datacenters[country][city].coordinates.lat
    ])
    .addTo(map);
Run Code Online (Sandbox Code Playgroud)

但是,我似乎对标记有某种偏移问题.问题是:当缩小一点时,标记的底部并没有真正指向确切的位置:

标记缩小了一点

当我进一步放大它到达目的地时,它指向确切的位置.

放大时标记

我真的很喜欢MapBox GL,但这个特殊的问题让我烦恼,我很想知道如何解决它.当这个问题得到解决时,我的实现远比我使用的原始映射软件更优越.

javascript mapbox mapbox-gl mapbox-gl-js

7
推荐指数
2
解决办法
4518
查看次数

Babel ESLint:TypeError:无法读取null的属性"range"

我用babel-eslint来修饰/修复我的代码.伟大的工作,直到我想采取一些ES2017 async await发现overhere.

我相应地改变了我的React应用程序,尽管有些不同:

我的index.js的相关部分:

async function renderApp() {
  const store = await configureStore()

  const history = syncHistoryWithStore(browserHistory, store, {
    selectLocationState: state => state.get('routing')
  })

  ReactDOM.render(
    <AppContainer>
      <MuiThemeProvider muiTheme={muiTheme}>
        <Provider store={store}>
          <Router history={history} routes={routes(store)} />
        </Provider>
      </MuiThemeProvider>
    </AppContainer>,
    document.getElementById('root')
  )
}


renderApp()
Run Code Online (Sandbox Code Playgroud)

我的商店:

// @flow

import 'babel-polyfill'

import { addFormSubmitSagaTo } from 'redux-form-submit-saga/es/immutable'
import { applyMiddleware, createStore, compose } from 'redux'
import { autoRehydrate, persistStore } from 'redux-persist-immutable'
import { browserHistory } from 'react-router'
import { combineReducers …
Run Code Online (Sandbox Code Playgroud)

babel async-await eslint ecmascript-2017

7
推荐指数
1
解决办法
5252
查看次数

节点服务器无法连接到postgres db

我最近从MySQL切换到postgres作为node.js项目的数据库.虽然我能够从我的本地pgAdmin III(OSX)客户端访问我的远程postgres数据库,但到目前为止我一直无法通过node.js连接到我的数据库.我确信我为pgAdmin和我的node.js输入的凭据完全相同.我尝试过的另一件事是在我的数据库服务器的pg_hba.conf中设置我的本地ipadress而不是md5.有什么我做错了吗?我最喜欢的搜索引擎提出了一些关于重置我的本地操作系统的令人担忧的命中.我刚刚使用了node-postgres的github repo doc中的示例:

var pg = require('pg');

var conString = "postgres://myusername:mypassword@hostname:5432/dbname";

var client = new pg.Client(conString);
client.connect(function(err) {
  if(err) {
    return console.error('could not connect to postgres', err);
  }
  client.query('SELECT NOW() AS "theTime"', function(err, result) {
    if(err) {
      return console.error('error running query', err);
    }
    console.log(result.rows[0].theTime);
    client.end();
  });
});
Run Code Online (Sandbox Code Playgroud)

这些是我每次尝试启动服务器时遇到的错误:

could not connect to postgres { [Error: getaddrinfo ENOTFOUND] code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo' }
Run Code Online (Sandbox Code Playgroud)

非常感谢帮助

postgresql node.js node-postgres

6
推荐指数
1
解决办法
8858
查看次数

在Sass更新3.4.6之后,Gulp Compass崩溃了

当我的gulp在我的sass文件上运行罗盘时,我遇到以下错误:

error src/scss/site/style.scss (/Library/Ruby/Gems/2.0.0/gems/sass-3.4.6/lib/sass/selector/abstract_sequence.rb:96:in `block in _specificity': undefined method `specificity' for [:not(.pointer)]:Array)
Run Code Online (Sandbox Code Playgroud)

我不知道这个错误究竟意味着什么,但它所指向的文件没有特殊的来源,它只是一个小的合法scss文件:

@import '../general';

.tld {
  color: $color-primary;
}
Run Code Online (Sandbox Code Playgroud)

我也一直在我的Gulp流中得到这些错误,它可能是相关的:

[21:45:01] Ignoring psych-2.0.6 because its extensions are not built.  Try: gem pristine psych-2.0.6

[21:45:01] Ignoring ffi-1.9.5 because its extensions are not built.  Try: gem pristine ffi-1.9.5
Run Code Online (Sandbox Code Playgroud)

当然,我尝试了"宝石原始",但这没有做任何事情.

我不知道发生了什么事,我知道我的gulpfile上周是一样的,它完美无缺.我安装了OSX约塞米蒂并没有做任何特别的事情.

我知道我的描述缺乏东西,但那是因为我不知道在哪里寻找解决方案,因为我不理解错误.

gulp osx-yosemite gulp-sass compass

5
推荐指数
1
解决办法
2160
查看次数

Pyenv activate 不使用 Fish Shell 运行激活脚本

我的 pyenv 工作正常,但它似乎没有运行位于以下位置的激活脚本/usr/local/var/pyenv/versions/project/bin/activate.fish

激活我的环境时,它会提供以下输出,但它不会回显激活脚本中的任何内容,这表明它没有运行。

dani@localhost ~/d/project> pyenv activate project
pyenv-virtualenv: prompt changing not working for fish.
Run Code Online (Sandbox Code Playgroud)

当然,我可以source手动更改该文件,但我太想找出它没有运行的原因。有某种调试模式吗?我不知道如何接近。

virtualenv fish pyenv

5
推荐指数
1
解决办法
4614
查看次数

等待将来完成

我使用postgres数据库查询来确定我的下一个操作.我需要等待结果才能执行下一行代码.现在我conn.query返回一个Future,但是当我将代码放在另一个函数中时,我无法将其设置为异步.

main() {
  // get the database connection string from the settings.ini in the project root folder 
  db = getdb();
  geturl().then((String url) => print(url));
}

Future geturl() {
  connect(db).then((conn) {
    conn.query("select trim(url) from crawler.crawls where content IS NULL").toList()
      .then((result) { return result[0].toString(); })
      .catchError((err) => print('Query error: $err'))
      .whenComplete(() {
        conn.close();
      });
  });
}
Run Code Online (Sandbox Code Playgroud)

我只是想geturl()等待返回的值,但无论我做什么; 它会立即发射.任何人都能指出我的一篇文档解释了我在这里缺少的东西吗?

dart dart-async

4
推荐指数
2
解决办法
1万
查看次数