小编Sha*_*son的帖子

es6类和带有事件处理程序的"this"

玩一些es6并遇到一个问题,我不知道如何解决.考虑以下

class Foo {
 constructor ( ) {
   window.addEventListener('scroll', this.watch);
 }

 watch ( ) {
   console.log(this);
 }
}
Run Code Online (Sandbox Code Playgroud)

在内部watch,thiswindow对象,正如预期的那样.但是我怎么称呼Foo?目前我用bind来解决这个问题,this.watch.bind(this)但我很想知道是否有更"适当"的ES6方式来实现这一目标.

javascript ecmascript-6

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

node.js&express - 应用程序结构的全局模块和最佳实践

我正在构建一个node.js应用程序,它是一个REST api,使用express和mongoose为我的mongodb.我现在已经完成了所有设置的CRUD端点,但我只想知道两件事.

  1. 如何扩展这种路由方式,具体来说,如何在路由之间共享模块.我希望我的每个路由都进入一个新文件,但显然只有一个数据库连接,你可以看到我在people.js的顶部包含了mongoose.

  2. 我是否必须在我的people.js中写出模型的模式3次?第一个模式定义了模型,然后我列出了createPerson和updatePerson函数中的所有变量.这感觉就像我在当天制作php/mysql CRUD一样哈哈.对于更新功能,我尝试编写一个循环来循环"p"以自动检测要更新的字段,但无济于事.任何提示或建议都会很棒.

此外,我喜欢整个应用程序的任何意见,对节点不熟悉,很难知道你做某事的方式是最有效或"最好"的做法.谢谢!

app.js

// Node Modules
var express     = require('express');
    app         = express();
    app.port    = 3000;



// Routes
var people      = require('./routes/people');

/*
var locations   = require('./routes/locations');
var menus       = require('./routes/menus');
var products    = require('./routes/products');
*/


// Node Configure
app.configure(function(){
  app.use(express.bodyParser());
  app.use(app.router);
});



// Start the server on port 3000
app.listen(app.port);



/*********
ENDPOINTS 
*********/

// People
app.get('/people', people.allPeople); // Return all people
app.post('/people', people.createPerson); // Create A Person
app.get('/people/:id', people.personById); // Return person by id
app.put('/people/:id', …
Run Code Online (Sandbox Code Playgroud)

javascript mongoose mongodb node.js express

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

无法让PUT在Nodejs/express上工作

我一直在写我的第一个节点应用程序,一个休息API.我已经把一切都运行得很好但PUT不会经历.我尝试了很多不同的组合,很多旧的教程和一些新的教程,我可能非常接近我只是不确定如何更新记录.此外,如果你看到任何跳出来的东西,请随时告诉我!我真的很喜欢节点,所以我很乐意学习最佳实践.

下面是我的表的路由,所有其他端点工作正常.如果您有任何疑问,请与我联系.

//Database
var mongoose = require("mongoose");
mongoose.connect('mongodb://Shans-MacBook-Pro.local/lantern/');


// Schema
var Schema = mongoose.Schema;  
var Person = new Schema({  
first_name: String,
last_name: String,
address: {
    unit: Number,
    address: String,
    zipcode: String,
    city: String,
    region: String,
    country: String
},
image: String, 
job_title: String,
created_at: { type: Date, default: Date.now },
active_until: { type: Date, default: null },
hourly_wage: Number,
store_id: Number,
employee_number: Number

});
var PersonModel = mongoose.model('Person', Person);  


// Return all people
exports.allPeople = function(req, res){
return PersonModel.find(function (err, person) …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js express

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

Mongoose 更新或插入多个文档

我正在尝试使用最新版本的 mongoose 插入一组对象,或者如果相应的产品 ID 已经存在则更新。我一生都无法找出正确的使用方法(bulkWrite、updateMany 等),而且我似乎无法在不出错的情况下弄清楚语法。例如,我正在尝试

Product.update({}, products, { upsert : true, multi : true }, (err, docs) => console.log(docs))
Run Code Online (Sandbox Code Playgroud)

这会引发错误 DeprecationWarning: collection.update is deprecated. Use updateOne, updateMany, or bulkWrite instead. MongoError: '$set' is empty. You must specify a field like so: {$set: {<field>: ...}

使用 updateMany 只会给我 $set 错误。我似乎找不到任何使用这种方法的人的例子,只是想知道是否有人可以为我提供一个如何正确使用它的例子。

明确地说,我生成了一个对象数组,然后我想将它们插入到我的数据库中,或者更新条目(如果它已经存在)。我要匹配的字段称为pid. 任何提示或建议将不胜感激!

编辑:

我的产品架构

const productSchema = new mongoose.Schema({
  title : String,
  image : String,
  price_was : Number,
  price_current : {
    dollars : String,
    cents : String
  }, …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js mongodb-query

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

禁用使用 express 节点服务器访问文件

我有一个在 CentOS 上运行的节点服务器,目前,您只需转到根目录中的任何文件并查看它,例如xxx.net/whatever.js. 这显然不是很好,因为我在某些文件中有一些敏感信息。

如何禁止用户从浏览器导航到这些文件?

我设置了一个 apache 代理来查看该站点

<VirtualHost *:80>
  ServerAdmin me@xxx.net
  ServerName www.xxx.net
  ServerAlias xxx.net

  DocumentRoot /var/www/xxx.net
  <Directory /var/www/xxx.net>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
  </Directory>

  ErrorLog /var/www/xxx.net/error.log
  CustomLog /var/www/xxx.net/requests.log combined

  ProxyRequests Off
  ProxyPreserveHost On
  ProxyVia Full
  <Proxy *>
   Require all granted
  </Proxy>

  <Location />
    ProxyPass http://127.0.0.1:4000/
    ProxyPassReverse http://127.0.0.1:4000/
  </Location>
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

然后 pm2 运行一个快速节点服务器。我已经尝试了各种 chmod'ing,但没有任何改变。如果有人能阐明我的问题,那就太好了。如果您需要更多信息,请告诉我,谢谢!

apache node.js express

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

画布像素化-调整像素大小

我有一个遍历图像的脚本。图像开始像素化,然后在查看时变为非像素化。我通过调用此函数x多次实现了requestAnimationFrame

Images.prototype.setPixels = function() {
    var sw          = this.imageWidth,
        sh          = this.imageHeight,
        imageData   = this.context.getImageData( 0, 0, sw, sh ),
        data        = imageData.data,
        y, x, n, m;

    for ( y = 0; y < sh; y += this.pixelation ) {
        for ( x = 0; x < sw; x += this.pixelation ) {

            var red = data[((sw * y) + x) * 4];
            var green = data[((sw * y) + x) * 4 + 1];
            var blue …
Run Code Online (Sandbox Code Playgroud)

javascript canvas html5-canvas

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