Tag: javascript

如何通过查询保留前N个文档并删除mongoDB中的所有其他文档?

我试图find一个最佳的方式来删除查找查询返回的顶部N以外的所有文档。 我们目前正在做的是得到所有的文档IDsorting和跳过N.然后发射删除查询为{id: {$in: idsList}} 。 1. model.find(findQuery).sort({_id:-1}).skip(N) -> collecting all ids from this query(idsList) 2. model.remove({_id:{'$in':idsList}}) 有没有办法使用单个查询以最佳方式做到这一点?

如何限制我的restapi只能使用node.js的引用URL?

我在nodejs中有一个restAPI,并会限制只从特定的URL访问? 我的node.js代码: var express=require("express"); var app=express(); var auth = require('basic-auth'); appStart.use(function(req, res, next) { console.log(req.get('origin')); console.log(requestIp.getClientIp(req)); console.log(req.ip); //check the api key username e password var authentication=auth(req); if(authentication.name!="apiTokenUsername" || authentication.pass!="apiTokenPassword") res.status(400).send("Api key Username or password incorrect!"); //check if the ip of request is authorized if(req.ip=="xxx.xxx.xxx.xxx") return next(); //check if the origin of request is authorized if(req.get('origin') […]

Socket.io – 通过https从客户端连接到服务器

我在我的虚拟服务器(Ubuntu)上创build了一个socket.io聊天应用程序,它作为systemd服务运行,并且正在运行。 我的server.js位于: /var/www/vhosts/mywebpage.de/w1.mywebpage.de/chat/ server.js看起来像这样: const io = require('socket.io')(3055); io.on('connection', function(socket) { // When the client emits 'addUser', this listens and executes socket.on('addUser', function(username, room) { … }); // When the client emits 'sendMessage', this listens and executes socket.on('sendMessage', function(msg) { … }); // Disconnect the user socket.on('disconnectUser', function(username, room) { … }); }); 在我的网站(https),我尝试连接如下: <script […]

在Array.map函数中使用await

在迭代数组之前,我需要等待一个asynchronous函数完成,我需要等待parsing的asynchronous函数如下: static async sendEmail (from, to, subject, text) { return new Promise((resolve, reject) => { let message = { from, to, subject, text }; AMMailing.transporter.sendMail(message, function (err, response) { if (err) { reject(err); } else { resolve(response); } }); }); } 这是我在数组中迭代的方式的代码,并试图等待它解决再次迭代之前: static sendEmailInQueue (queue) { queue.map(async (person, index) => { console.log('sending email to: ', […]

如何将Getter / Setter应用到对象

在定义它的时候,如何将一个getter / setter应用到一个对象本身? 例如,我想做 myobj = 123运行console.log(123) 我知道这是可能的,通过使用class extends ,但我想find一个更简单的方式, 可能在构造函数中定义getter / setter。 有谁知道如何? 这甚至有可能吗? 如果您需要更多信息,或者我不够清楚,请告诉我。 – JacobTDC

使用GitHub API编辑和添加提交消息到文件

我正在使用npm GitHub API 。 我有四块数据。 参考我想要更新的文件 我要更新的文件的path 我想要在这个文件中的新内容 我想要编辑的提交消息 另外,我可以对API进行身份validation,并有权访问此回购。 我现在如何编辑这个文件并推送这个提交? const GitHub = require('github-api') const gh = new GitHub({ token: config.app.git_token, }, githubUrl) const repo = gh.getRepo(config.app.repoOwner, config.app.repoName) repo.getRef(`heads/${config.app.repoBranch}`).then((response) => { const ref = response.data.object.sha const path = 'README.md' const content = '#Foo Bar\nthis is foo bar' const message = 'make readme foo bar' […]

nodejs – 如何使用node-opcua访问远程opc服务器?

我在github中find了一个更好的项目节点opcua,使用这个项目的简单例子可以创build一个opc服务器和客户端,并且在本地可以访问,我现在需要访问一个远程的opc服务器,提供几个参数是:IPnetworking节点(例如10.195.156.150),opc服务(例如ArchestrA FSGateway),opc组(如ArchestrA),opc TagName(例如HXi1_P201_00。Ia),我可以使用node-opcua? 谢谢! sample_server代码: var opcua = require("node-opcua"); var os = require("os"); // Let's create an instance of OPCUAServer var server = new opcua.OPCUAServer({port: 4334}); var siteName = 'site01'; var var1 = {nodeId: 'ns=4;s=hook_weight', browseName: 'hook_weight', dataType: 'Double'}; var var2 = {nodeId: 'ns=4;s=rpm', browseName: 'rpm', dataType: 'Double'}; var var3 = {nodeId: 'ns=1;s=free_memory', browseName: 'free_memory', […]

无视观察者并不反映这种变化

我有一个特定path中的4个文件夹,其中包含一个单独的文件,其中包含要忽略的文件和扩展名列表。 我使用npm模块忽略观察者, 我面对的问题是,我在任何文件夹中改变了什么,在改变后没有被反映出来,只有四个文件夹中的一个跟踪改变。 其次,当任何更改事件被触发时,忽略观察者继续为所有文件夹运行。 下面我详细解释一下。 我有4个项目文件夹:比方说 /path/to/project1 /path/to/project2 /path/to/project3 /path/to/project4 每个文件夹由两个文件组成,我想在每个项目文件夹的单独文件中提供这些文件。 /path/to/project1/.no_include /path/to/project2/.no_include /path/to/project3/.no_include /path/to/project4/.no_include 我做了一个单独的文件在每个项目的一个单独的path,我做了改变,它会反映,将触发一个事件的变化,所以要做到这一点,我已经configuration了忽略观察者模块。 config.js var ignoringWatcher = require('ignoring-watcher').createWatcher({ dirs: [ '../../path/to/project1', '../../path/to/project2', '../../path/to/project3', '../../path/to/project4', ], ignoreFiles: [ '../../path/to/project1/.no_include', '../../path/to/project2/.no_include', '../../path/to/project3/.no_include', '../../path/to/project4/.no_include' ] }); ignoringWatcher .on('modified', function (eventArgs) { // Fired for any change event (add, delete, etc.) var type = eventArgs.type; // […]

得到谷歌日历假日nodejs

我想获得谷歌日历假期 此function来自https://developers.google.com/google-apps/calendar/quickstart/nodejs 只用我的calendarID工作……….. function listEvents(auth) { var calendar = google.calendar('v3'); calendar.events.list({ auth: auth, calendarId: 'en.thai#holiday@group.v3.calendar.google.com', timeMin: (new Date()).toISOString(), maxResults: 10, singleEvents: true, orderBy: 'startTime' }, function(err, response) { if (err) { console.log('The API returned an error: ' + err); return; } var events = response.items; if (events.length == 0) { console.log('No upcoming events found.'); } […]

如何与僵尸asynchronousJavaScripttesting工作?

这个问题我创build了一个应用程序,与expressionjs和我试图用僵尸来testing它。 我已经写了一些testing来检查网页上的内容。 我已经手动检查了页面,并且正确地从我们的数据库中加载了内容。 但是,当我运行我们的testing时,我得到不可预知的结果。 这些是我们的testing。 第一个testing是失败的。 before(function(done) { models.Listing.create({name: "Studio Flat in London", text: "This is a great flat in zone one."}); models.Listing.create({name: "The Fish Market", text: "description"}); done(); }); beforeEach(function(done) { browser.visit('/', done); }); it('each property should have a title', function(done) { expect(browser.html("body")).to.include("Studio Flat in London"); }); it('each property should have a title the […]