小编chi*_*zui的帖子

带有 formdata 的 Express body-parser req.body 是空对象

不知何故我的 req.body 总是空的,也许你有一个想法:

这是我的服务器代码:

const Express = require('express');
const bodyParser = require('body-parser');

const app = new Express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.post('/save', (req, res) => {
  console.log(req.body)  // => {}
  res.send(req.body);
});

const env = process.env.NODE_ENV || 'production';
app.listen(3000, err => {
   if (err) { return console.error(err); }
   console.info(`Server running on http://localhost:${port} [${env}]`);
});
Run Code Online (Sandbox Code Playgroud)

当我尝试使用 javascript 发送 formdata 时,req.body 为空:

const data = new FormData(document.querySelector('form'));
console.log(data);  // seems empty already??? FormData{}??
const xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost:3000/save');
xhr.send(data); …
Run Code Online (Sandbox Code Playgroud)

javascript form-data node.js express body-parser

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

如何在Puppeteers .evaluate()方法中传递函数?

每当我尝试传递一个函数时,就像这样:

var myFunc = function() { console.log("lol"); };

await page.evaluate(func => {
 func();
 return true;
}, myFunc);
Run Code Online (Sandbox Code Playgroud)

我得到:

(node:13108) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Evaluation failed: TypeError: func is not a function
at func (<anonymous>:9:9)
(node:13108) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Run Code Online (Sandbox Code Playgroud)

为什么?如何正确做?

谢谢!

€:让我澄清一下:我这样做是因为我想先找到一些DOM元素,然后在该函数内部使用它们,更像这样(简化):

var myFunc = function(element) { element.innerHTML = "baz" };

await page.evaluate(func => { …
Run Code Online (Sandbox Code Playgroud)

javascript google-chrome node.js puppeteer

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

怎么处理导入地狱的反应?

我通过Node运行我的反应应用程序.有没有办法轻松处理这个导入地狱?

我在跑

./node_modules/.bin/babel-node --presets react,es2015 server/server.js
Run Code Online (Sandbox Code Playgroud)

作为npm开始.而server.js是一个简单的Express服务器,服务于ReactDOMServer.renderToString(<MyApp />)

我的一些反应组件有这样的东西:

import GenericTemplate from "../../templates/GenericTemplate/GenericTemplate";
import Footer from "../../organisms/Footer/Footer";
import Header from "../../organisms/Header/Header";
import Hero from "../../organisms/Hero/Hero";
import MainMenu from "../../organisms/MainMenu/MainMenu";
import TodoList from "../../organisms/TodoList/TodoList";
Run Code Online (Sandbox Code Playgroud)

这很容易出错,像目录名这样的更改会导致手动输入每个文件来更新它.

你知道我怎么解决这个问题吗?理想情况下,我会有这样的事情:

import { Footer, Header, Hero, MainMenu, TodoList } from "myComponents"
Run Code Online (Sandbox Code Playgroud)

那可能吗?怎么样?

谢谢!

javascript import require node.js reactjs

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

检测系统范围的按键?(node.js?)

即使我的节点应用程序不在焦点上,也可以检测到按键吗?

过去有一种用于魔兽的工具,可以将鼠标快速移至某个位置并在按下键盘按钮时触发单击。该工具可能仍可在Windows上运行,但我计划为Mac(甚至使用node.js甚至整个系统)构建类似的东西,而不是特定于魔兽。

我在Google上进行了大量搜索,找到了几种移动鼠标并以nodemacmouserobojs形式发送键的工具,但是我找不到任何东西来检测系统范围的键盘输入(这是至关重要的,因为移动仅应在按键时进行)。这似乎在可可中是可行的但是节点解决方案将是完美无缺的。可能吗?怎么样?谢谢。

javascript macos node.js

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

在three.js 中创建非常柔和的阴影?

是否可以在three.js 中创建一个非常柔和/非常微妙的阴影?喜欢这张照片吗? 在此处输入图片说明

到目前为止我所做的一切是这样的:

在此处输入图片说明

我的灯:

hemisphereLight = new THREE.HemisphereLight(0xaaaaaa,0x000000, 0.9);
ambientLight = new THREE.AmbientLight(0xdc8874, 0.5);
shadowLight = new THREE.DirectionalLight(0xffffff, 1);
shadowLight.position.set(5, 20, -5);
shadowLight.castShadow = true;
shadowLight.shadowCameraVisible = true;
shadowLight.shadowDarkness = 0.5;
shadowLight.shadow.camera.left = -500;
shadowLight.shadow.camera.right = 500;
shadowLight.shadow.camera.top = 500;
shadowLight.shadow.camera.bottom = -500;
shadowLight.shadow.camera.near = 1;
shadowLight.shadow.camera.far = 1000;
shadowLight.shadowCameraVisible = true;
shadowLight.shadow.mapSize.width = 4096; // default is 512
shadowLight.shadow.mapSize.height = 4096; // default is 512
Run Code Online (Sandbox Code Playgroud)

并渲染:

renderer.shadowMapEnabled = true;
renderer.shadowMapSoft = true;
renderer.shadowMapType = THREE.PCFSoftShadowMap;
Run Code Online (Sandbox Code Playgroud)

感谢您

javascript shadow three.js

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

如何使用 puppeteer 将“page”元素传递给函数?

我正在尝试从函数内检查页面上是否有元素可用,如果元素在页面上,很好,继续执行代码,如果没有,请记录错误。

使用try puppeteer 页面,这是我尝试过的:

const browser = await puppeteer.launch();
const page = await browser.newPage();

const check = element => {
  try {
    await page.waitFor(element, {timeout: 1000});
  } catch(e) {
    console.log("error : ", e)
    await browser.close();
  }
}

await page.goto('https://www.example.com/');
check("#something");

console.log("done")
await browser.close();
Run Code Online (Sandbox Code Playgroud)

我明白了Error running your code. SyntaxError: Unexpected identifier。我调试了一下,似乎pagecheck函数中是意外的标识符。所以我试图像这样用力传递它:

const browser = await puppeteer.launch();
const page = await browser.newPage();

const check = (element, page) => {
  try {
    await page.waitFor(element, {timeout: …
Run Code Online (Sandbox Code Playgroud)

javascript google-chrome puppeteer

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

是否可以在没有 React 的情况下使用 MobX?

我喜欢 MobX。我想在原生 JavaScript 中使用它。我尝试添加 CDN https://cdnjs.com/libraries/mobx 然后我尝试使用 MobX 语法编写一个类:

class MyStore {
  @observable data = 'foo'
}
const myStore = new MyStore();
Run Code Online (Sandbox Code Playgroud)

但我收到错误:

SyntaxError: illegal character
Run Code Online (Sandbox Code Playgroud)

对于@和 :

ReferenceError: exports is not defined
Run Code Online (Sandbox Code Playgroud)

从内部mobx.js文件。

所以如果没有 React 和 Blunding/Transpiler,这似乎是不可能的,是吗?如果没有,还有其他选择吗?

谢谢你!

javascript mobx

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

Angular:使用$ rootScope.$ on vs $ scope.$ on来捕获$ rootScope.$在一个组件中广播 - 在性能方面哪个更好?

嗨,我想知道在性能方面哪些更好.假设我有这个广播东西的工厂:

angular.module('core.foo')
  .factory('Foo',
    ['$rootScope', 
      function FooFactory($rootScope) {

        $rootScope.$broadcast('bar', baz);

      }
    ]
  );
Run Code Online (Sandbox Code Playgroud)

并在某个地方(或很多人)监听该事件.什么会更好?

要使用$ rootScope.$ on:

angular.module('foo').component('foo', {
  templateUrl: 'foo.html',
  controller: ['$rootScope',
    function FooController($rootScope) {

      $rootScope.$on('bar', function(event, data){
        // use the data
      });

    }]
});
Run Code Online (Sandbox Code Playgroud)

或$ scope.$ on:

angular.module('foo').component('foo', {
  templateUrl: 'foo.html',
  controller: ['$scope',
    function FooController($scope) {

      $scope.$on('bar', function(event, data){
        // use the data
      });

    }]
});
Run Code Online (Sandbox Code Playgroud)

两者都有效,我只是好奇.

angularjs angularjs-scope angularjs-rootscope

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

如何访问 Web 组件上的 ObservedAttributes

因此,在 Web 组件中,一旦您指定了要观察的属性,您就可以使用attributeChangedCallback :

static get observedAttributes() { return ['myAttribute']; }
Run Code Online (Sandbox Code Playgroud)

如何从我的子类中列出/访问观察到的属性?

class Foo extends HTMLElement {
    connectedCallback() {
        console.log(this.observedAttributes); // <= undefined
    }
}

class Bar extends Foo {
    static get observedAttributes() {
        return ['bar'];
    }
}
Run Code Online (Sandbox Code Playgroud)

这有可能吗?是否有针对观察到的属性的吸气剂?

我认为这里的困难在于获取observedAttributes父类的。因为如果它在同一个班级,你就可以Foo.observedAttributes像@javimovi提到的那样。

这里有一个jsbin可以玩。

谢谢你!

html javascript web-component ecmascript-6 es6-class

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