小编nul*_*ull的帖子

在expressjs路由中使用socket.io而不是在main server.js文件中

我有表达/ nodejs api.我正在添加socket.io功能.目前我的所有路由都在单独的文件夹中,我将它们包含在server.js文件中,并将它们用作app.use()函数.

在server.js文件中,我还通过监听特定端口(如3000)启动快速服务器.

let server = app.listen(3000);
Run Code Online (Sandbox Code Playgroud)

根据所有谷歌搜索,我发现我需要传递服务器变量来初始化socket.io,如下所示.

let io = require('socket.io')(server);
Run Code Online (Sandbox Code Playgroud)

现在的问题是,因为它需要这个变量,那么如何在我的路径文件中使用socket.io,这些文件位于不同的文件夹中以从客户端发出和接收事件?

UPDATE

在server.js文件中

let route = require('./routes/route');

let app = express();

let server = app.listen(3000);

console.log('Listening to port');

let io = require('socket.io')(server);

app.use('/api/1.0/events', route(io));
Run Code Online (Sandbox Code Playgroud)

在route.js文件中

let express = require('express');

module.exports = (io) => {
    console.log('IO: ', io);
};
Run Code Online (Sandbox Code Playgroud)

更新2

server.js文件

let express = require('express');
let events = require('./routes/events');
let app = express();
let server = app.listen(3000);

let io = require('socket.io')(server);


app.use(function(request, response, next) {
    request.io = io; …
Run Code Online (Sandbox Code Playgroud)

javascript sockets node.js express

10
推荐指数
3
解决办法
6347
查看次数

解析错误:意外的标记,预期的“,”

我在react js中有以下代码。

class Posts extends Component {
  render() {
    return (
      {console.log('test')}
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

运行此代码后,我收到错误

Parsing error: Unexpected token, expected ","

   8 |     return (
>  9 |       {console.log('test')}
     |       ^
  10 |     );
  11 |   }
  12 | }
Run Code Online (Sandbox Code Playgroud)

更新

用父标签包装它会返回相同的错误

>  9 |       <div>
     |       ^
  10 |         {console.log('nothing')}
  11 |       </div>
Run Code Online (Sandbox Code Playgroud)

更新

这是全班同学

import React, { Component } from 'react';
import { connect } from 'react-redux';

class Post extends Component {
  render() {
    return (
      <div> …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs

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

错误:exportArchive:找不到“io.ionic.starter”的配置文件

我正在尝试构建我的 ionic 3 应用程序并在我的设备上部署和实时重新加载,因此我正在尝试以下命令:

ionic cordova run ios --device --prod -lcs
Run Code Online (Sandbox Code Playgroud)

但运行该命令后,我收到以下错误:

error: exportArchive: No profiles for 'io.ionic.starter' were found
Run Code Online (Sandbox Code Playgroud)

"No profiles for 'io.ionic.starter' were found" UserInfo={NSLocalizedDescription=No profiles for 'io.ionic.starter' were found, NSLocalizedRecoverySuggestion=Xcode couldn't find any iOS App Development provisioning profiles matching 'io.ionic.starter'. Automatic signing is disabled and unable to generate a profile. To enable automatic signing, pass -allowProvisioningUpdates to xcodebuild.}
Run Code Online (Sandbox Code Playgroud)

它说To enable automatic signing, pass -allowProvisioningUpdates to xcodebuild.但是在科尔多瓦怎么样?

一些额外的错误输出。正如您在下面看到的,首先Archive Succeeded但后来失败了。

** ARCHIVE SUCCEEDED ** …
Run Code Online (Sandbox Code Playgroud)

cordova ionic-framework ionic3 angular

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

如何在html5/ionic中使用平方米或平方公里

这就是我的想法.我想用GPS获取用户位置.我想要计算平方公里或米,而不是发送当前位置.

----------------------------------
|                                |
|                                |
|                                | 
|                                |
|              .                 |
|              ^                 |
|      My current position       |
|                                |
|                                |
|                                |
----------------------------------
Run Code Online (Sandbox Code Playgroud)

正如您在上图中所看到的,我现在的位置,但我想计算HTML5或Ionic周围的整个区域将是更优选的.

更新

在此输入图像描述

在上面的图像中,红点是我的位置,我需要将整个区域放在红色矩形中.获取数据库中的那个商店.我查看了多边形区域公式,但这需要几个顶点,地理位置我只获得经度和纬度两个坐标.我怎么用这两点呢?

UPDATE

我在这里找到了一个解决方案,但是这个解决方案是跟踪用户的当前位置,并且在calculateDistance功能上公式使用当前位置(经度和纬度)和跟踪位置(经度和纬度),即Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * Math.sin(dLon/2) * Math.sin(dLon/2);

这对我的第二个场景非常好,但首先我不想跟踪用户的位置.首先,我简单地获取当前用户位置(经度和纬度),从中计算区域并将其发送到服务器.现在我不确定我将如何实现这一目标.有帮助吗?

javascript html5 gps geolocation ionic-framework

8
推荐指数
1
解决办法
333
查看次数

显示从相机拍摄的图像

如下所示,我正在使用该[src]属性.我要做的是预览从设备的相机拍摄的图像.请参阅下面的其他打字稿代码.

<img [src]="lastImage" style="width: 100%" [hidden]="lastImage === null">
<button ion-button icon-left (click)="presentActionSheet()">
    <ion-icon name="camera"></ion-icon>Select Image
</button>
Run Code Online (Sandbox Code Playgroud)

这是.ts代码

lastImage: string = null;

public presentActionSheet() {
    let actionSheet = this.actionSheetCtrl.create({
        title: 'Select Image Source',
        buttons: [
            {
                text: 'Load from Library',
                handler: () => {
                    this.takePicture(this.camera.PictureSourceType.PHOTOLIBRARY);
                }
            },
            {
                text: 'Use Camera',
                handler: () => {
                    this.takePicture(this.camera.PictureSourceType.CAMERA);
                }
            },
            {
                text: 'Cancel',
                role: 'cancel'
            }
        ]
    });

    actionSheet.present();
}

public takePicture(sourceType) {
    // Create options for the Camera Dialog
    var …
Run Code Online (Sandbox Code Playgroud)

javascript cordova ionic-framework ionic3 angular

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

(未知网址)的Http失败响应:0 Android上的未知错误

我有一个离子应用程序,当我在iOS上运行它然后它工作得很好但是当我在Android上运行时我得到这个错误

Http failure response for (unknown url): 0 Unknown Error
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我得到这个的任何理由?我在服务器端允许CORS,因为它在iOS设备上运行.

有帮助吗?

编辑

这就是我在app.js文件中的内容

const cors = require('cors');
Run Code Online (Sandbox Code Playgroud)

然后我只使用默认选项.

app.use(cors());
Run Code Online (Sandbox Code Playgroud)

android node.js cordova ionic-framework angular

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

Node JS读取表单数据

我想将一些属性和文件发送到具有multipart / form-data的Node JS应用程序。

客户端HTML表单:

<input id="picName" name="picName" class="form-control" placeholder="PicTitle..." style="border-radius: 1%; margin: 1%" type="name">
<form id="frmUploader" enctype="multipart/form-data" action="/post/picture/" method="post">
<input id="file" type="file" name="picUploader" multiple /><br>
<input class="btn btn-md btn-success" type="submit" name="submit" id="btnSubmit" value="Upload" /><br>
</form>
Run Code Online (Sandbox Code Playgroud)

客户端JS:

$('#frmUploader').submit(function () {
      var username = localStorage.getItem("userName");
      var categoryName = $( "#categoryAddPic option:selected").text();
      var picTitle = $('#picName').val();

          var picture = $('input[name="picUploader"]').get(0).files[0];
          var formData = new FormData();
          formData.append('picture', picture);
          formData.append('username', username);
          formData.append('categoryName', categoryName);
          formData.append('picTitle', picTitle);

            $.ajax({
                method: 'POST',
                url: 'http://localhost:3000/post/picture',
                data: formData,
                headers:{
                    "Authorization": …
Run Code Online (Sandbox Code Playgroud)

javascript node.js

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

CSS3 旋转摆动

我有下面的代码,我正在尝试将图像旋转 360 度。但旋转会摇晃。

.pully {
  position: absolute;
  right: 3.7em;
  bottom: 1em;
  z-index: 11;
  animation-name: clockwiseSpinner;
  animation-timing-function: ease-in;
  animation-iteration-count: infinite;
  animation-duration: 1s;
}

.line {
  position: absolute;
  right: 145px;
  bottom: 10px;
  height: 200px;
  border-right: 2px solid red;
}

@-webkit-keyframes clockwiseSpinner {
  from {-webkit-transform: rotate(0deg)}
  to {-webkit-transform: rotate(360deg)}
}
Run Code Online (Sandbox Code Playgroud)
<div class="line"></div>
<img class="pully" src="https://s8.postimg.cc/vax8le4x1/p-pully.png" alt="">
Run Code Online (Sandbox Code Playgroud)

JSFiddle

知道如何消除这种不稳定吗?

css css-animations

0
推荐指数
1
解决办法
1157
查看次数